It is possible to do this using “dot syntax” without using “eval” (evil)? (I know I can do with this[methodtocall]())
var myObj = {
method1 : function(){
return 1;
},
method2 : function(){
return 1;
},
callMethod : function(methodtocall){
this.+methodtocall+()
},
init : function(){
this.callMethod("method1");
}
}
myObj.init();
No, aside from
evalor an equivalent, it’s not possible with the dot notation member operator.If you want to maintain consistent syntax, then always use the square bracket notation of the member operator.