Created
August 2, 2014 03:03
-
-
Save Alex1990/fc2acefbcc127a99c431 to your computer and use it in GitHub Desktop.
My `Array.prototype.forEach` 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-02 | |
*/ | |
/** | |
* 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/forEach | |
*/ | |
// test | |
// Array.prototype.forEach = void 0; | |
if (typeof Array.prototype.forEach != 'function') { | |
Array.prototype.forEach = function(callback, thisArg) { | |
if (typeof this.length != 'number') return; | |
if (typeof callback != 'function') return; | |
if (typeof this == 'object') { | |
for (var i = 0; i < this.length; i++) { | |
if (i in this) { | |
callback.call(thisArg || this, this[i], i, this); | |
} else { | |
return; | |
} | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment