向数组添加函数 [英] Adding functions to an array

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

问题描述

此代码将函数添加到数组:

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

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