From 223323ac827eb235a2b884d17a40c7afc1f6feb6 Mon Sep 17 00:00:00 2001 From: rolux Date: Fri, 25 May 2012 15:16:21 +0200 Subject: [PATCH] add Array.prototype.reduceRight --- source/Ox/js/Fallback.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/source/Ox/js/Fallback.js b/source/Ox/js/Fallback.js index 2046b5a3..56788cdd 100644 --- a/source/Ox/js/Fallback.js +++ b/source/Ox/js/Fallback.js @@ -99,13 +99,13 @@ if (!Array.prototype.map) { // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce if (!Array.prototype.reduce) { - Array.prototype.reduce = function reduce(fn, ret) { + Array.prototype.reduce = function(fn, ret) { if (this === void 0 || this === null || typeof fn !== 'function') { throw new TypeError(); } var arr = Object(this), i, - len = this.length; + len = arr.length; if (!len && ret === void 0) { throw new TypeError(); } @@ -113,7 +113,32 @@ if (!Array.prototype.reduce) { ret = arr[0]; i = 1; } - for (i = i || 0; i < len ; ++i) { + for (i = i || 0; i < len; i++) { + if (i in arr) { + ret = fn.call(void 0, ret, arr[i], i, arr); + } + } + return ret; + }; +} + +// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduceRight +if (!Array.prototype.reduceRight) { + Array.prototype.reduceRight = function(fn, ret) { + if (this === void 0 || this === null || typeof fn !== 'function') { + throw new TypeError(); + } + var arr = Object(this), + i, + len = arr.length; + if (!len && ret === void 0) { + throw new TypeError(); + } + if (ret === void 0) { + ret = arr[len - 1]; + i = len - 2; + } + for (i = i || len - 1; i >= 0; i--) { if (i in arr) { ret = fn.call(void 0, ret, arr[i], i, arr); }