Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created August 2, 2014 03:03
Show Gist options
  • Save Alex1990/fc2acefbcc127a99c431 to your computer and use it in GitHub Desktop.
Save Alex1990/fc2acefbcc127a99c431 to your computer and use it in GitHub Desktop.
My `Array.prototype.forEach` polyfill.
/**
* Author: Alex Chao(alexchao1990@gmail.com)
* 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