Javascript 向数组中添加函数
我正在向数组中添加一个匿名函数,并尝试在该数组上迭代执行其内容。即使使用一个简单的测试用例,我也会得到一个TypeError:不是函数 我错过了一些简单的东西吗Javascript 向数组中添加函数,javascript,anonymous-function,Javascript,Anonymous Function,我正在向数组中添加一个匿名函数,并尝试在该数组上迭代执行其内容。即使使用一个简单的测试用例,我也会得到一个TypeError:不是函数 我错过了一些简单的东西吗 //an array of functions var signInFunctions = []; //add a function to the array signInFunctions.push(function() { console.log("hello world"); }); function userSign
//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();
这是一个函数数组,因此首先需要访问该索引处的函数,然后调用它:
而不是
signInFunctions(i)代码>使用符号函数[i]()代码>您需要签名功能[i]()
trysignInFunctions[i]()代码>
TypeError: 'function () {
console.log("hello world");
}' is not a function (evaluating 'signInFunctions(i)')
signInFunctions[i]();