开发者

JavaScript: What is the equivalent of PHP's "$this->$somefunc()"?

开发者 https://www.devze.com 2022-12-17 04:31 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消