javascript原生foreach方法重写

var arr = ['张三','李四','王五','王琦','马六','八路']

Array.prototype.forEach = function(fun,desc){
var arrleng = this.length;
if(desc == 'order'){
for(let i = arrleng-1 ; i >= 0; i--){
fun(this[i],i,this);
}
}else{
for(let i = 0; i < arrleng; ++i){
fun(this[i],i,this);
}
}
}
arr.forEach((item,index)=>{
console.log(item);
},'order')

posted @ 2022-03-09 16:08  Dreams_log  阅读(80)  评论(0)    收藏  举报