JavaScript: What is the equivalent of PHP's "$this->$somefunc()"?
Is it possible to stor开发者_开发问答e the name of a function as string in JS, and invoke it from an object, pretty much like the PHP code below?
$this->$someFunc();
this[someFunc]();
Sure thing. Try this:
var f = "foo";
var result = obj[f]();
where foo
is a method on obj
, or
this[f]();
where foo
is a method on the current instance.
精彩评论