Created
March 24, 2015 10:18
-
-
Save MatthewBarker/29a2db79083e9a3b3e19 to your computer and use it in GitHub Desktop.
Filter polyfill - taken from MDN for use in JSfiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!Array.prototype.filter) { | |
Array.prototype.filter = function(fun/*, thisArg*/) { | |
'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 thisArg = arguments.length >= 2 ? arguments[1] : void 0; | |
for (var i = 0; i < len; i++) { | |
if (i in t) { | |
var val = t[i]; | |
// NOTE: Technically this should Object.defineProperty at | |
// the next index, as push can be affected by | |
// properties on Object.prototype and Array.prototype. | |
// But that method's new, and collisions should be | |
// rare, so use the more-compatible alternative. | |
if (fun.call(thisArg, 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