var OrderDetails = function () {
var orderdetails = {};
orderdetails.doSomething = function(){
alert('do something');
};
return orderdetails;
}
代码中的其他地方......
processMethod('doSomething')
function processMethod(strMethod)
{
// strMethod = 'doSomething';
var orderdet = OrderDetails(); //not the exact instantiation, just illustrating it is instantiated
orderdet.strMethod(); //this is the line I'm stuck with.
}
我正在尝试通过Javascript中的String名称来调用方法。我已经看过apply,call和eval()作为这个问题的潜在解决方案,但似乎无法使它们中的任何一个工作。对于我的特定对象场景,是否有任何关于语法的指导?
答案 0 :(得分:6)
使用bracket notation代替dot notation:
orderdet[strMethod]();
答案 1 :(得分:1)
这应该有效。 processMethod( 'doSomething的')
function processMethod(strMethod)
{
// strMethod = 'doSomething';
var orderdet = OrderDetails(); //not the exact instantiation, just illustrating it is instantiated
orderdet[strMethod](); //this is the line I'm stuck with.
}