Skip to content

Instantly share code, notes, and snippets.

@tapmodo
Created October 22, 2014 23:24
Show Gist options
  • Save tapmodo/6cf4b1a8ec991e3f74f2 to your computer and use it in GitHub Desktop.
Save tapmodo/6cf4b1a8ec991e3f74f2 to your computer and use it in GitHub Desktop.
Array.filter and Array.map Javascript Polyfills
// Array.map polyfill
if (Array.prototype.map === undefined) {
Array.prototype.map = function(fn) {
var rv = [];
for(var i=0, l=this.length; i<l; i++)
rv.push(fn(this[i]));
return rv;
};
}
// Array.filter polyfill
if (Array.prototype.filter === undefined) {
Array.prototype.filter = function(fn) {
var rv = [];
for(var i=0, l=this.length; i<l; i++)
if (fn(this[i])) rv.push(this[i]);
return rv;
};
}
@AjayPoshak
Copy link

This solution is very risky.
In both polyfills, you are not even doing a sanity check for function supplied as the argument.
And please check the proper signature of filter method.

@PolluxLee
Copy link

I agree with @AjayPoshak.

@localhostdotdev
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment