admin管理员组文章数量:1401215
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
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
Share Improve this question edited Sep 4, 2015 at 21:20 Barmar 784k57 gold badges548 silver badges660 bronze badges asked Jan 4, 2010 at 23:21 salmanesalmane 4,84713 gold badges50 silver badges61 bronze badges 1-
for (functionname in action)
doesn't do what you think;functionname
will be properties of the Array, which may not be in numerical order and can return other properties depending on browser. Never usefor...in
on Array. Instead use the plain old C-style loopfor(var i= 0; i<array.length; i++)
. – bobince Commented Jan 5, 2010 at 0:10
3 Answers
Reset to default 3obj[action[functionname]]();
will do the trick.
You should be able to do this.
obj[action[functionName]]();
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.
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".
本文标签: Javascript How do i call functions from an array of function namesStack Overflow
版权声明:本文标题:Javascript How do i call functions from an array of function names - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744276278a2598426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论