add Array.prototype.every and Array.prototype.some

This commit is contained in:
rolux 2012-05-25 15:24:27 +02:00
parent 223323ac82
commit aebd6aacff

View file

@ -1,5 +1,25 @@
'use strict'; 'use strict';
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
if (!Array.prototype.every) {
Array.prototype.every = function(fn, that) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length >>> 0,
ret = true;
for (i = 0; i < len; i++) {
if (i in arr && !fn.call(that, arr[i], i, arr)) {
ret = false;
break;
}
}
return ret;
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
if (!Array.prototype.filter) { if (!Array.prototype.filter) {
Array.prototype.filter = function(fn, that) { Array.prototype.filter = function(fn, that) {
@ -97,6 +117,26 @@ if (!Array.prototype.map) {
}; };
} }
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
if (!Array.prototype.some) {
Array.prototype.some = function(fn, that) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length >>> 0,
ret = false;
for (i = 0; i < len; i++) {
if (i in arr && fn.call(that, arr[i], i, arr)) {
ret = true;
break;
}
}
return ret;
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
if (!Array.prototype.reduce) { if (!Array.prototype.reduce) {
Array.prototype.reduce = function(fn, ret) { Array.prototype.reduce = function(fn, ret) {