[英]Javascript How do i call functions from an array of function names
var action = ['function1','function2' etc ]
var obj = new objectcreate ();
for (functionname in action){
obj+'.'+action[variablename]+'()';
}
the functions are already generated I just wanna go through an array and execute all the functions it on the object i created thanks for your help函数已经生成我只想通过一个数组并在我创建的对象上执行它的所有函数,谢谢你的帮助
您应该能够做到这一点。
obj[action[functionName]]();
obj[action[functionname]]();
will do the trick. 会成功的
None of the answers above worked for me.上面的答案都不适合我。
Noting that we are 10.5 years on from the original post the chances are JS have evolved.请注意,我们距原始帖子已有 10.5 年的时间,因此 JS 可能已经发展。
My solution is:我的解决办法是:
window[action[functionName]]();
A solution tailored to the original question is as follows:针对原始问题的解决方案如下:
var action = ['test1','test2'];
function test1(){
alert("test 1 running");
}
function test2(){
alert("test 2 running");
}
for (var i=0; i<action.length; i++){
window[action[i]]();
}
Expected output is an alert box saying "test 1 running" followed by another alert box saying "test 2 running".预期的输出是一个警告框说“测试 1 正在运行”,然后是另一个警告框说“测试 2 正在运行”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.