Javascript 函数数组 [英] Javascript Array of Functions
本文介绍了Javascript 函数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
谢谢!
推荐答案
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');
这篇关于Javascript 函数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文