Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Last active August 29, 2015 14:04
Show Gist options
  • Save Alex1990/7dda873e64404ecaca15 to your computer and use it in GitHub Desktop.
Save Alex1990/7dda873e64404ecaca15 to your computer and use it in GitHub Desktop.
This is my `Array.prototype.map` polyfill.
/**
* Author: Alex Chao(alexchao1990@gmail.com)
* Date: 2014-08-01
*/
/**
* Note: You can choose the other polyfill which is compatible with ECMA-262, 5th edition
* Url: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
*/
// test
// Array.prototype.map = void 0;
// For array and array-like objects
if (typeof Array.prototype.map != 'function') {
Array.prototype.map = function(callback, thisArg) {
if (typeof this.length != 'number') return;
if (typeof callback != 'function') return;
var newArr = [];
if (typeof this == 'object') {
for (var i = 0; i < this.length; i++) {
if (i in this) {
newArr[i] = callback.call(thisArg || this, this[i], i, this);
} else {
return;
}
}
}
return newArr;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment