Array.prototype.each

Array.prototype.each = function(closure){
    //递归合并
    return this.length ? [closure(this.slice(0,1))].concat(this.slice(1).each(closure)) : [];
}
[1,2,3,4].each(function(x){return x*2}); //2,4,6,8
[1,2,3,4].each(function(x){return x-1}); //0,1,2,3

 

posted on 2013-08-21 10:31  Lecaf  阅读(386)  评论(0编辑  收藏  举报