向数组添加函数 [英] Adding functions to an array
问题描述
此代码将函数添加到数组:
This code adds functions to an array :
var fArr = []
fArr.push(test())
fArr.push(test())
function test(){
console.log('here')
}
但是,每次将函数test()
添加到数组fArr
However the function test()
is invoked each time its added to array fArr
是否可以在不调用功能的情况下将功能test()
添加到数组fArr
中?
Can the function test()
be added to the array fArr
without invoking the function ?
我试图用函数填充数组fArr
,然后遍历fArr
并调用这些函数,而不是在将函数添加到当前行为fArr
时调用这些函数.
I'm attempting to populate the array fArr
with functions and then iterate over fArr
and invoke the functions, not invoke the functions as they are being added to fArr
which is current behavior.
小提琴: http://jsfiddle.net/3fd83oe1/
推荐答案
是的,您可以向数组添加函数.您实际上在做的是调用函数,并将函数调用的结果添加到数组中.您想要的是什么
Yes, you can add functions to an array. What you're actually doing is invoking the functions and adding the result of the function invocation to the array. What you want is this:
fArr.push(test);
fArr.push(test);
这篇关于向数组添加函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!