问题
Consider I have a name of a function which does not require any argument in a var -
var fn = "foo";
Can I execute it in some or similar like this -
eval(fn);
It does not work. Please suggest.
My definition of function will look like this -
function foo() {
....do something....
}
回答1:
try this
eval(fn)();
or this
eval(fn + "()");
回答2:
Please do not use eval.
If the function is in global scope, simply do
var fn = "foo";
window[fn]();
DEMO
来源:https://stackoverflow.com/questions/16183571/is-there-a-way-to-execute-a-function-when-i-have-its-name-in-a-string