//myForeach 数组每个元素都执行一次回调函数 // (typeof Array.prototype.forEach==="undefined")&&(Array.prototype.forEach = function(callback){ // for(var i = 0 ; i < this.length ; i++){ // var element = this[i]; // callback(element,i,this); // } // }); (Array.prototype.mforEach = function (callback, thisArg) { var T, k; if (this == null) { throw new TypeError("this is null or not defined"); } var O = Object(this); var len = O.length >>> 0; if (typeof callback !== "function") { throw new TypeError(callback + " is not a function"); } if (arguments.length > 1) { T = thisArg; } k = 0; while (k < len) { var kValue; if (k in O) { kValue = O[k]; callback.call(T, kValue, k, O); } k++; } }); //myEvery 检测数值元素的每个元素是否都符合条件 (typeof Array.prototype.every === "undefined") && (Array.prototype.every = function (callback) { for (var i = 0; i < this.length; i++) { var item = this[i]; if (!callback(item, i, this)) { return false; } } return true; }); //mySome 检测数组元素中是否有元素符合指定条件 (typeof Array.prototype.some === "undefined") && (Array.prototype.some = function (callback) { for (var i = 0; i < this.length; i++) { var item = this[i]; if (callback(item, i, this)) { return true; } } return false; }); //myFilter 检测数值元素,并返回符合条件所有元素的数组 (typeof Array.prototype.filter === "undefined") && (Array.prototype.filter = function (callback) { var arr = []; var temp = 0; for (var i = 0; i < this.length; i++) { var item = this[i]; if (callback(item, i, this)) { arr[temp] = item; temp++; } } return arr; }); //myReduce 将数组元素计算为一个值(从左到右) (typeof Array.prototype.reduce === "undefined") && (Array.prototype.reduce = function (callback, initialValue) { var num = 0; var total; if (initialValue !== undefined) { total = initialValue; } else { total = this[0]; num = 1; } for (var i = num; i < this.length; i++) { var item = this[i]; total = callback(total, item, i, this); } return total; }); // (typeof Array.prototype.push === "undefined") && (Array.prototype.push=function () { // for (var i=0;i