开发者

Javascript How do i call functions from an array of function names

开发者 https://www.devze.com 2022-12-15 22:34 出处:网络
var action = [\'function1\',\'functio开发者_JAVA技巧n2\' etc ] var obj = new objectcreate (); for (functionname in action){
var action = ['function1','functio开发者_JAVA技巧n2' 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]](); 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".

0

精彩评论

暂无评论...
验证码 换一张
取 消