我可以动态调用方法名称为字符串的对象方法吗?我会这样想象:
var FooClass = function() {
this.smile = function() {};
}
var method = "smile";
var foo = new FooClass();
// I want to run smile on the foo instance.
foo.{mysterious code}(); // being executed as foo.smile();
最佳答案
如果属性名称存储在变量中,请使用[]
foo[method]();
关于Javascript从字符串动态调用对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9854995/