currying in javascript


/ Published in: JavaScript
Save to your folder(s)

each in Array,It can operate each element in Array.
e.g.
var result = [1,2,3,4].each(function(x){return Math.sqrt(x);});
//it return sqrt in [1,2,3,4]'s each item


Copy this code and paste it in your HTML
  1. Array.prototype.each = function(fn){
  2. return this.length? [fn(this[0])].concat(this.slice(1).each(fn)): [];
  3. };
  4. //e.g.
  5. var arr = [1,2,3,4].each(function(x){return x*2;});
  6. //alert(arr);//[2,3,6,8]

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.