ECMAScript5 新特性(四)
?
?
?
?
?
?当浏览器没有实现此方法时,可以用以下方式代替
?
if (!Array.prototype.every) { Array.prototype.every = function(fun /*, thisp */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t && !fun.call(thisp, t[i], i, t)) return false; } return true; };}
?
?
?
?
?
?
当浏览器不支持的时候,你可以用以下代码代替
?
if (!Array.prototype.some) { Array.prototype.some = function(fun /*, thisp */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t && fun.call(thisp, t[i], i, t)) return true; } return false; };}
?
?
?
?
?当浏览器没有实现的时候,你可以通过如下方法代替
?
?
?
if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t) fun.call(thisp, t[i], i, t); } };}?
?
?
Sample2
?
?
var numbers = [1, 4, 9];var roots = numbers.map(Math.sqrt);// roots is now [1, 2, 3]// numbers is still [1, 4, 9]
如果浏览器没有实现,则可以用如下方法代替
?
if (!Array.prototype.map) { Array.prototype.map = function(fun /*, thisp */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var res = new Array(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t) res[i] = fun.call(thisp, t[i], i, t); } return res;};
?
?
?
?
?如果浏览器没有实现,则可以用如下方式代替:
?
if (!Array.prototype.filter) {Array.prototype.filter = function(fun /*, thisp */) {"use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var res = []; var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t) { var val = t[i]; // in case fun mutates this if (fun.call(thisp, val, i, t)) res.push(val); }}return res;};}?
?
?
?
如果浏览器没有实现,则可用以下代码代替
?
?
?
?
?
if (!Array.prototype.reduce) { Array.prototype.reduce = function(fun /*, initialValue */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); // no value to return if no initial value and an empty array if (len == 0 && arguments.length == 1) throw new TypeError(); var k = 0; var accumulator; if (arguments.length >= 2) { accumulator = arguments[1]; } else { do { if (k in t) { accumulator = t[k++]; break; } // if array contains no values, no initial value to return if (++k >= len) throw new TypeError(); } while (true); } while (k < len) { if (k in t) accumulator = fun.call(undefined, accumulator, t[k], k, t); k++; } return accumulator;};}?
?
?
if (!Array.prototype.reduceRight) { Array.prototype.reduceRight = function(callbackfn /*, initialValue */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof callbackfn !== "function") throw new TypeError(); // no value to return if no initial value, empty array if (len === 0 && arguments.length === 1) throw new TypeError(); var k = len - 1; var accumulator; if (arguments.length >= 2) { accumulator = arguments[1]; } else { do { if (k in this) { accumulator = this[k--]; break; } // if array contains no values, no initial value to return if (--k < 0) throw new TypeError(); } while (true); } while (k >= 0) { if (k in t) accumulator = callbackfn.call(undefined, accumulator, t[k], k, t); k--; }return accumulator; };}?
?
1 楼 panxi620 2011-05-11 虽然是教学,但是能否下次贴字上来大些,很伤眼睛