处理数组的forEach
1 //forEach处理 2 if(!Array.prototype.forEach) { 3 Array.prototype.forEach = function (callback) { 4 for(var i=0,len=this.length;i<len;i++) { 5 callback(this[i], i); 6 } 7 } 8 }
处理数组的map
1 //处理map 2 if(!Array.prototype.map) { 3 Array.prototype.map = function (callback) { 4 var arr = []; 5 for(var i=0,len=this.length;i<len;i++) { 6 arr.push(callback(this[i], i)); 7 } 8 return arr; 9 } 10 }
//处理数组的filter
1 //处理filter 2 if(!Array.prototype.filter) { 3 Array.prototype.filter = function (callback) { 4 var arr = []; 5 for(var i=0,len=this.length;i<len;i++) { 6 if(callback(this[i], i)) { 7 arr.push(this[i]) 8 } 9 } 10 return arr; 11 } 12 }