Last active
August 29, 2015 14:04
-
-
Save Alex1990/7dda873e64404ecaca15 to your computer and use it in GitHub Desktop.
This is my `Array.prototype.map` polyfill.
This file contains 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
/** | |
* Author: Alex Chao([email protected]) | |
* 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