Skip to content

Commit

Permalink
added fallback impls of Array.prototype.{forEach,map,filter}
Browse files Browse the repository at this point in the history
  • Loading branch information
rsms committed Aug 2, 2010
1 parent d29444e commit 002806c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions oui/std-additions/array.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ if (typeof Array.isArray !== 'function') {
Array.isArray = function(obj) { return toString.call(obj) === "[object Array]"; };
}

if (!Array.prototype.forEach) {
Array.prototype.forEach = function(block, ctx) {
var len = this.length >>> 0;
for (var i = 0; i < len; ++i) {
if (i in this) {
block.call(ctx, this[i], i, this);
}
}
};
}
if (!Array.prototype.map) {
Array.prototype.map = function(fun, ctx) {
var len = this.length >>> 0, res = new Array(len);
for (var i = 0; i < len; ++i) {
if (i in this) {
res[i] = fun.call(ctx, this[i], i, this);
}
}
return res;
};
}
if (!Array.prototype.filter) {
Array.prototype.filter = function (block, ctx) {
var values = [];
for (var i = 0; i < this.length; i++) {
if (block.call(ctx, this[i])) {
values.push(this[i]);
}
}
return values;
};
}

/**
* Return the first true return value from fun which is called for each value.
* fun is called on this and receives a single argument (current value).
Expand Down

0 comments on commit 002806c

Please sign in to comment.