In my app.js
I have defined functions like this:
var func1 = function(v1,v2,v3) {
.
.
}
var func2 = function(v1,v2,v3) {
.
.
}
In my controller.js
:
var action = "";
if(..) { action = 'func1';}
else {action = 'func2'}
action(a1,a2,a3);
However, I get an error, saying that action is not a function. How can I access func1
and func2`` like this?
try removing quotes, like, change:
if(..) { action = 'func1';}
else {action = 'func2'}
to
if(..) { action = func1;}
else {action = func2;}
Added: you are assigning string to your action variable when you add quotes for the func1
, you can check it doing:
var func1 = function(v1,v2,v3) {
alert("first");
};
var test_this = func1;
console.log( typeof test_this); //gives you function
where as
var test_this = 'func1'; //with quotes added
console.log( typeof test_this); //gives you string
You're trying to access the function from a string. I'm no expert but you could just try
if (..) {
func1(a1, a2, a3);
} else {
func2(a1, a2, a3);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.