ECMAScript5 新特性(四)
?
if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(searchElement /*, fromIndex */) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (len === 0) return -1; var n = 0; if (arguments.length > 0) { n = Number(arguments[1]); if (n !== n) n = 0; else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) n = (n > 0 || -1) * Math.floor(Math.abs(n)); } if (n >= len) return -1; var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); for (; k < len; k++) { if (k in t && t[k] === searchElement) return k; } return -1; };}Function 17: Array.prototype.lastIndexOf用法和16相似,取得最后一次出现参数的index如果浏览器没有实现此方法,你可以通过这种方式实现if (!Array.prototype.lastIndexOf) { Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (len === 0) return -1; var n = len; if (arguments.length > 0) { n = Number(arguments[1]); if (n !== n) n = 0; else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) n = (n > 0 || -1) * Math.floor(Math.abs(n)); } var k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); while (k >= 0) { if (k in t && t[k] === searchElement) return k; } return -1; };}
?
?
?
function isBigEnough(element, index, array) {return (element >= 10);}var passed = [12, 5, 8, 130, 44].every(isBigEnough);// passed is falsepassed = [12, 54, 18, 130, 44].every(isBigEnough);// passed is true
?
?当浏览器没有实现此方法时,可以用以下方式代替
?
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; };}?
?
?
function isBigEnough(element, index, array) { return (element >= 10);}var passed = [2, 5, 8, 1, 4].some(isBigEnough);// passed is falsepassed = [12, 5, 8, 1, 4].some(isBigEnough);// passed is 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; };}?
?
?
function printElt(element, index, array) { print("[" + index + "] is " + element); // assumes print is already defined}[2, 5, 9].forEach(printElt);// Prints:// [0] is 2// [1] is 5// [2] is 9
?
?当浏览器没有实现的时候,你可以通过如下方法代替
?
?
?
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); } };}??
function makePseudoPlural(single) { return single.replace(/o/g, "e");}var singles = ["foot", "goose", "moose"];var plurals = singles.map(makePseudoPlural);// plurals is ["feet", "geese", "meese"]// singles is unchanged?
?
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;};?
?
?
function isBigEnough(element, index, array) {return (element >= 10);}// 12, 130, 44var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
?
?如果浏览器没有实现,则可以用如下方式代替:
?
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;};}??
var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });// total == 6var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {return a.concat(b);});// flattened is [0, 1, 2, 3, 4, 5]?
?
?
如果浏览器没有实现,则可用以下代码代替
?
?
?
?
?
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;};}??
?
var total = [0, 1, 2, 3].reduceRight(function(a, b) { return a + b; });//total == 6var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) { return a.concat(b);}, []);// flattened is [4, 5, 2, 3, 0, 1]?如果浏览器没有实现,则可以用如下代码代替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 虽然是教学,但是能否下次贴字上来大些,很伤眼睛