我创建了一个具有如下布局的 Javascript 对象:
var myObject : {
doSomething : function (args) {
//do some stuff
return result;
}
//note the args for both functions are of the same format
doSomethingElse : function (args){
//do some other stuff
return otherResult
}
}
现在我想实现以下目标,但不确定如何以干净的方式实现:
console.log(myObject(args));
/*
Output:
{
doSomething : result,
doSomethingElse : otherResult
}
*/
我希望将这两个函数分开,因为我希望能够在不同的实例中引用它们,但也同时评估这两个函数以获得所需的输出,如上所示。
预先感谢您的帮助!
最佳答案
我想您必须进行迭代,例如:
var results = {},
args = "some args";
for (var key in myObject) {
if (typeof myObject[key] === "function") results[key] = myObject[key](args);
}
console.log(results); //should be the output you want (untested)
关于javascript - 评估 Javascript 对象中的所有函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24891947/