Javascript Array of Functions Javascript Array of Functions arrays arrays

Javascript Array of Functions


var array_of_functions = [    first_function,    second_function,    third_function,    forth_function]

and then when you want to execute a given function in the array:

array_of_functions[0]('a string');


I think this is what the original poster meant to accomplish:

var array_of_functions = [    function() { first_function('a string') },    function() { second_function('a string') },    function() { third_function('a string') },    function() { fourth_function('a string') }]for (i = 0; i < array_of_functions.length; i++) {    array_of_functions[i]();}

Hopefully this will help others (like me 20 minutes ago :-) looking for any hint about how to call JS functions in an array.


Without more detail of what you are trying to accomplish, we are kinda guessing. But you might be able to get away with using object notation to do something like this...

var myFuncs = {  firstFunc: function(string) {    // do something  },  secondFunc: function(string) {    // do something  },  thirdFunc: function(string) {    // do something  }}

and to call one of them...

myFuncs.firstFunc('a string')