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)')
2
  • 4
    You need signInFunctions[i](); Commented Mar 30, 2013 at 17:05
  • try signInFunctions[i](); Commented Mar 30, 2013 at 17:07

2 Answers 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]();
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.