Javascript array of functions

  • user warning: Table 'allaboutwebstuff.comments' doesn't exist query: SELECT COUNT(*) FROM comments c WHERE c.nid = 66 AND c.status = 0 in /home/albertoperego/allaboutwebstuff.com/modules/comment/comment.module on line 992.
  • user warning: Table 'allaboutwebstuff.comments' doesn't exist query: SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.thread, c.status FROM comments c INNER JOIN users u ON c.uid = u.uid WHERE c.nid = 66 AND c.status = 0 ORDER BY c.thread DESC LIMIT 0, 50 in /home/albertoperego/allaboutwebstuff.com/modules/comment/comment.module on line 992.

The fun part of Javascript is that functions are variables and at a first glance it might look a weird thing but when you get used to this concept you realize it's a great thing.
Let's see with the following example how to create an array of functions and then fire them sequentially when needed.

var arrayF = [
    [console.log, ['test1']],
    [console.log, ['test2', 'test3']],
    [console.error,['test4']]
];

arrayF.push([alert, ['test5']]);

for(i in arrayF)
    arrayF[i][0].apply(window, arrayF[i][1]);

Handy, isn't it? This thing can be very useful when you need to execute a procedure which include several existing functions.
Also, you could create an array of functions and a caller, which could look like:

function caller(start, end){
  for(i=start;i<end;i++)
    arrayF[i][0].apply(window, arrayF[i][1]);

and then invoke a subset of functions only.

Tags:
Share and Enjoy: