We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 12/02/07


Tagged

data array function


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

vali29
korzhik


Array Functions as Data


Published in: JavaScript 


The important thing to notice is the () operator in a[2]. It is invoking the function inside a[0] with the argument a[1].

  1. function functionsDontNeedNames(){
  2. var a = new Array(3);//create array length 3
  3. a[0] = function(x){return x*x};//this is now the function
  4. a[1] = 4;//will be used as 'x'
  5. a[2] = a[0](a[1]);//a[2] now equals 16!
  6. }

Report this snippet 

You need to login to post a comment.