1

Normally we can do this:

var somefunc = function(){
    this.func1 = function(){ .... }
    this.func2 = function(){ .... }
};
myvar = new somefunc();

Then we can call myvar.func1() to run the function.

But, just my thinking. Is it possible to store functions into array and then we can just run all the functions in the array by loop through the array? If possible, then how can I push a function into array?

Thank you.

5
  • 2
    It is very tempting to post an answer that only says 'Yes it is'. Commented Apr 8, 2014 at 14:13
  • To answer "how can I push a function into array?", with Array.prototype.push, just like you would with any other item you can push into an array, since functions are first-class citizens in JavaScript. Commented Apr 8, 2014 at 14:17
  • 1
    Why would you ask such a question instead of trying it out!? Even if you don't have node or don't know the dev tools console, you could just create a little fiddle. Why!? Commented Apr 8, 2014 at 14:20
  • I tried but it didn't work, so I thought it cannot be done. Anyway, after referring to some of example code here, I manage to get it works. Thanks a lot for everyone help here. Really appreciate it. Commented Apr 8, 2014 at 14:23
  • In such cases it is better if you post your (failed) attempt. Commented Apr 8, 2014 at 14:31

5 Answers 5

8

Sure you can, something like this

var arr = [
    function() {
        console.log('fn 1')
    },
    function() {
        console.log('fn 2')
    },
    function() {
        console.log('fn 3')
    }
]

arr.forEach(function(el) {
    el();
});

FIDDLE

Works perfectly fine, the same with objects or anywhere else you'd like to store a function, as functions are just objects

to push another function to the array, you'd do

arr.push(
    function() {
        console.log('fn 4')
    }
);

easy as that

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your answer. I try with your example and it works fine until I test with IE8. On IE8, error point to arr.forEach(function(el) {}, with message: Object doesn't support this property or method How can I get it works on IE8? Thank you.
3

Functions in javascript are first class citizens, so the answer is yes.

From wikipedia: "In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures."

Comments

3

Yes, it is possible. Have you tried it? It works just like you would expect:

var funcs = [
  function (){ console.log('foo'); },
  function (){ console.log('bar'); },
];

for (var i = 0; i < funcs.length; i++) {
  funcs[i]();
}

Comments

2

Yes you can :

var lx = [
        function(x){ console.log(x) },
        function(y){ console.log(y) }
    ];

for(var i=0; i<lx.length; i++)
    lx[i](i);

Comments

1

Yes

var myArrayOFunctions = [
    function() { .... },
    function() { .... }
]

myArrayOFunctions[0]();   // runs the first function

Example: http://jsfiddle.net/6VB6Y/

The nice part of using an array (versus using an object) is that you can loop through them in order with a simple for loop, whereas using a for...in on an object does not guarantee the order. Also, you can push new functions into your array easily:

myArrayOFunctions.push(function() { .... });

So you can provide a simple way to add hooks in your code where somebody using your code can attach new functions to execute when some event happens.

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.