没有特别难的知识点,直接上代码;Talk is cheap,show me the code!
var arr = [1,2,3,[4,5,[6,7,8,[9,10]]]];
Array.prototype.eachall = function(fn) {
try{
//声明一个计数器
this.i || (this.i = 0)
//判断数组存在且传的值是一个函数
if (this.length>0 && fn.constructor==Function) {
//循环
while(this.i
var e = this[this.i];
//判断当前值是不是数组如果是数组进行递归否则执行回调函数
if (e && e.constructor == Array) {
e.eachall(fn);
} else{
fn.call(e,e);
};
this.i++;
}
//变量回收
this.i=null;
};
}catch(err){
console.log("have error:"+err);
}
};
//使用
arr.eachall(function(item){
alert(item);
});