最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript: store functions as array - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Apr 8, 2014 at 14:11 user1995781user1995781 19.5k45 gold badges139 silver badges241 bronze badges 5
  • 2 It is very tempting to post an answer that only says 'Yes it is'. – Aioros 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. – ajp15243 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!? – schlingel 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. – user1995781 Commented Apr 8, 2014 at 14:23
  • In such cases it is better if you post your (failed) attempt. – Felix Kling Commented Apr 8, 2014 at 14:31
Add a ment  | 

5 Answers 5

Reset to default 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

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

From wikipedia: "In puter 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."

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]();
}

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

Yes

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

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

Example: http://jsfiddle/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.

发布评论

评论列表(0)

  1. 暂无评论