首页 > 编程 > JavaScript > 正文

为原生js Array增加each方法

2019-11-20 23:25:19
字体:
来源:转载
供稿:网友
复制代码 代码如下:

   Array.prototype.each = function(fn)
{
return this.length ? [fn(this.slice(0,1))].concat(this.slice(1).each(fn)) : [];
};

[1,2,3,4].each(function(x){
document.write(x + "<br/>");
});
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表