功能的JavaScript数组 [英] Javascript Array of Functions

查看:124
本文介绍了功能的JavaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  VAR array_of_functions = [
    first_function('字符串')​​,
    second_function('字符串')​​,
    third_function('字符串')​​,
    forth_function('字符串')
]array_of_functions [0];

如预期,因为创建阵列当执行所述阵列中的每个函数即不起作用。

什么是做在数组中执行任何功能的正确方法:

  array_of_functions [0]; //或,array_of_functions [1]等。

谢谢!


解决方案

  VAR array_of_functions = [
    first_function,
    second_function,
    third_function,
    forth_function
]

,然后当你想在阵列中执行特定的功能:

  array_of_functions [0]('字符串')​​;

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!

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆