<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>遍历数组</title>
</head>
<body>
<script>
var arr = [1,2,3,[4,[5]]];
Array.prototype.each = function(fn){
try{
this.i || (this.i = 0);
if(this.length > 0 && fn.constructor == Function){
while(this.i < this.length){
var e = this[this.i];
if(e && e.constructor == Array){
e.each(fn);
}else{
// fn.apply(e,[e]);
fn.call(e,e);
}
this.i++;
}
this.i = null;
}
}catch(err){
alert(err)
}
}
arr.each(function(item){
alert(item)
})
</script>
</body>
</html>
javascript原型遍历数组
最新推荐文章于 2024-01-15 15:23:50 发布