简体   繁体   English

Javascript 如何从函数名称数组调用函数

[英]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.

相关问题 Titanium如何从函数名称数组中调用函数? - Titanium How do i call functions from an array of function names? 如何在Javascript中正确调用匿名函数? - How do I correctly call a functions of anonymous function in Javascript? 如何通过使用onclick事件中的函数并使用循环从函数中的数组中计数函数来调用多个JavaScript函数? - How to call multiple JavaScript functions by using function in onclick event and using loop to count functions from array in function? 在Javascript中,如果有一个对象具有很多作为函数的属性,那么如何将它们转换为(函数名称的)字符串数组? - In Javascript, if there is an object with a lot of properties that are functions, how do you convert them to array of strings (of the function names)? 如何调用我从 JavaScript 中的数组中提取的函数? - How do I call the function I pull out from an array in JavaScript? 如何在 javascript 函数之外调用数组 - How do I call an array outside of a javascript function 如何从 Javascript/Google App 脚本中的另一个函数调用数组 - How do I call an array from another function in Javascript/ Google App script JavaScript-如何从字符串名称调用函数并传递数组对象? - JavaScript - How do I call a function from a string name and pass an array object? 如何从JavaScript调用angularjs函数? - How do i call angularjs function from javascript? 如何从 javascript 调用静态 Typescript 类函数? - How do I call a static Typescript class function from javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM