Array.prototype.each = function(fn){
try{
if(this.length > 0 && fn.constructor == Function){
this.i || (this.i = 0);
for(this.i;this.i < this.length; this.i++){
var ele = this[this.i];
if(ele && ele.constructor == Array){
arguments.callee.call(ele,fn);
}else{
fn.call(ele,ele);
}
}
this.i = null;
}
}catch(ex){
}
}
var arr = [1,2,3,[4,[5,[6]]]];
arr.each(function(ele){
console.log(ele);
});
// 输出为:
// 1
// 2
// 3
// 4
// 5
// 6
21、自己实现each 方法可遍历多维数组
最新推荐文章于 2022-10-13 11:00:15 发布