Skip to content

Instantly share code, notes, and snippets.

@josher19
Created October 18, 2011 06:35
Show Gist options
  • Save josher19/1294754 to your computer and use it in GitHub Desktop.
Save josher19/1294754 to your computer and use it in GitHub Desktop.
/**
* Array.prototype.map Polyfill
*/
Array.prototype.map = function(fun, thisp) {
"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 = [ ];
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i]; // in case fun mutates this
res[i] = thisp ? fun.call(thisp, val, i, t) : fun(val, i, t);
}
}
return res;
};
/* End of file map.js */
/*
// add to polyfill.js :
// Array.prototype.map
map: {
test: function() {
return (!! Array.prototype.map);
}
},
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment