是否可以使用“点语法”而不使用“eval”(邪恶)来做到这一点? (我知道我可以使用 this[methodtocall]()
)
var myObj = {
method1 : function(){
return 1;
},
method2 : function(){
return 1;
},
callMethod : function(methodtocall){
this.+methodtocall+()
},
init : function(){
this.callMethod("method1");
}
}
myObj.init();
最佳答案
不,除了 eval
或等价物,点符号成员运算符是不可能的。
如果要保持语法一致,请始终使用成员运算符的方括号表示法。
callMethod : function(methodtocall){
this[methodtocall]()
},
init : function(){
this["callMethod"]("method1");
}
关于javascript - 有什么方法可以使用点语法按名称调用方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11129642/