javascript - 向数组添加函数

标签 javascript anonymous-function

我正在向一个数组添加一个匿名函数,并尝试迭代该数组来执行其内容。即使是一个简单的测试用例,我也会收到 TypeError: is not a function。

我是不是漏掉了一些简单的东西?

//an array of functions
var signInFunctions = [];

//add a function to the array
signInFunctions.push(function() {
    console.log("hello world");
});

function userSignedIn() {
    //execute all functions in the signInFunctions array 
    for (var i = 0; i < signInFunctions.length; i++) {
        signInFunctions(i);
    }
}

userSignedIn();

这是错误:

TypeError: 'function () {
console.log("hello world");
}' is not a function (evaluating 'signInFunctions(i)')

最佳答案

这是一个函数数组,所以首先您需要访问该索引处的函数,然后然后调用它:

signInFunctions[i]();

关于javascript - 向数组添加函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15721143/

相关文章:

javascript - 当网站在非 Flash 浏览器中打开时重定向到 html 页面

python - 根据正则表达式选择要调用的 Python 函数

JavaScript 命名排序函数

Javascript:访问匿名函数中的函数

javascript用数组全局替换

javascript - 在Vuejs中动态切换flex大小?

javascript - 用于切换使用 AngularJs 从下拉列表中选择特定选项的按钮

c - 使用 GCC 语句表达式的匿名函数

javascript - javascript匿名函数的生命周期是多少?

javascript - 在 2checkout 中提交之前验证表单