3

I am adding an anonymous function to an array and attempting to iterate over that array executing its contents. Even with a simple test case, I am getting a TypeError: is not a function.

Am I missing something simple?

//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();

Here's the error:

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

2 回答 2

6

This is an array of functions, so first you need to access the function at that index and then call it:

signInFunctions[i]();
于 2013-03-30T17:05:46.467 回答
0

instead of signInFunctions(i); use signInFunctions [i] ();

于 2013-03-30T17:06:34.303 回答