Skip to content

Instantly share code, notes, and snippets.

@trevmex
Created August 18, 2011 15:01
Show Gist options
  • Save trevmex/1154241 to your computer and use it in GitHub Desktop.
Save trevmex/1154241 to your computer and use it in GitHub Desktop.
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun, thisp) {
"use strict";
if (typeof this === "undefined" || this === null) {
throw new TypeError();
}
if (typeof fun !== "function") {
throw new TypeError();
}
thisp = thisp || this;
var t = this.slice(),
len = t.length,
res = [],
i,
val;
for (i = 0; i < len; i += 1) {
if (typeof t[i] === "undefined") {
val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment