Javascript 函数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4908378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Array of Functions
提问by Nick
var array_of_functions = [
first_function('a string'),
second_function('a string'),
third_function('a string'),
forth_function('a string')
]
array_of_functions[0];
That does not work as intended because each function in the array is executed when the array is created.
这不会按预期工作,因为在创建数组时会执行数组中的每个函数。
What is the proper way of executing any function in the array by doing:
通过执行以下操作来执行数组中的任何函数的正确方法是什么:
array_of_functions[0]; // or, array_of_functions[1] etc.
Thanks!
谢谢!
回答by Darin Dimitrov
var array_of_functions = [
first_function,
second_function,
third_function,
forth_function
]
and then when you want to execute a given function in the array:
然后当你想在数组中执行给定的函数时:
array_of_functions[0]('a string');
回答by pjcabrera
I think this is what the original poster meant to accomplish:
我认为这就是原始海报要完成的任务:
var array_of_functions = [
function() { first_function('a string') },
function() { second_function('a string') },
function() { third_function('a string') },
function() { fourth_function('a string') }
]
for (i = 0; i < array_of_functions.length; i++) {
array_of_functions[i]();
}
Hopefully this will help others (like me 20 minutes ago :-) looking for any hint about how to call JS functions in an array.
希望这会帮助其他人(比如 20 分钟前的我:-) 寻找有关如何在数组中调用 JS 函数的任何提示。
回答by Jeff
Without more detail of what you are trying to accomplish, we are kinda guessing. But you might be able to get away with using object notation to do something like this...
如果没有关于您要完成的任务的更多详细信息,我们只是在猜测。但是您可能可以使用对象表示法来做这样的事情......
var myFuncs = {
firstFunc: function(string) {
// do something
},
secondFunc: function(string) {
// do something
},
thirdFunc: function(string) {
// do something
}
}
and to call one of them...
并打电话给其中之一...
myFuncs.firstFunc('a string')
回答by Robin
Or just:
要不就:
var myFuncs = {
firstFun: function(string) {
// do something
},
secondFunc: function(string) {
// do something
},
thirdFunc: function(string) {
// do something
}
}
回答by Gus
I would complement this thread by posting an easier way to execute various functions within an Array using the shift()
Javascript method originally described here
我将通过发布一种使用最初描述的shift()
Javascript 方法在数组中执行各种函数的更简单方法来补充这个线程
var a = function(){ console.log("this is function: a") }
var b = function(){ console.log("this is function: b") }
var c = function(){ console.log("this is function: c") }
var foo = [a,b,c];
while (foo.length){
foo.shift().call();
}
回答by Mensur Gri?evi?
It's basically the same as Darin Dimitrov's
but it shows how you could use it do dynamically create and store functions and arguments.
I hope it's useful for you :)
它与基本相同,Darin Dimitrov's
但它展示了如何使用它动态创建和存储函数和参数。我希望它对你有用:)
var argsContainer = ['hello', 'you', 'there'];
var functionsContainer = [];
for (var i = 0; i < argsContainer.length; i++) {
var currentArg = argsContainer[i];
functionsContainer.push(function(currentArg){
console.log(currentArg);
});
};
for (var i = 0; i < functionsContainer.length; i++) {
functionsContainer[i](argsContainer[i]);
}
回答by mlhazan
up above we saw some with iteration. Let's do the same thing using forEach:
在上面我们看到了一些迭代。让我们使用 forEach 做同样的事情:
var funcs = [function () {
console.log(1)
},
function () {
console.log(2)
}
];
funcs.forEach(function (func) {
func(); // outputs 1, then 2
});
//for (i = 0; i < funcs.length; i++) funcs[i]();
回答by Thomas Gotwig
Execution of many functions through an ES6 callback
通过 ES6 回调执行许多函数
const f = (funs) => {
funs().forEach((fun) => fun)
}
f(() => [
console.log(1),
console.log(2),
console.log(3)
])
回答by Daniel Lizik
If you're doing something like trying to dynamically pass callbacks you could pass a single object as an argument. This gives you much greater control over which functions you want to you execute with any parameter.
如果您正在尝试动态传递回调之类的操作,则可以将单个对象作为参数传递。这使您可以更好地控制要使用任何参数执行哪些功能。
function func_one(arg) {
console.log(arg)
};
function func_two(arg) {
console.log(arg+' make this different')
};
var obj = {
callbacks: [func_one, func_two],
params: ["something", "something else"];
};
function doSomething(obj) {
var n = obj.counter
for (n; n < (obj.callbacks.length - obj.len); n++) {
obj.callbacks[n](obj.params[n]);
}
};
obj.counter = 0;
obj.len = 0;
doSomething(obj);
//something
//something else make this different
obj.counter = 1;
obj.len = 0;
doSomething(obj);
//something else make this different
回答by Leonardo Ciaccio
This is correct
这是对的
var array_of_functions = {
"all": function(flag) {
console.log(1+flag);
},
"cic": function(flag) {
console.log(13+flag);
}
};
array_of_functions.all(27);
array_of_functions.cic(7);