Skip to content

Instantly share code, notes, and snippets.

@EarMaster
Created December 19, 2012 10:01
Show Gist options
  • Save EarMaster/4335676 to your computer and use it in GitHub Desktop.
Save EarMaster/4335676 to your computer and use it in GitHub Desktop.
polyfill for forEach array iteration function
/**
* Polyfill for forEach array iteration function
*
* @see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
*/
if (!Array.prototype.forEach) {
Array.prototype.forEach = function forEach(callback, thisArg) {
var T, k;
if (this==null)
throw new TypeError( "this is null or not defined" );
var O = Object(this);
var len = O.length >>> 0;
if ({}.toString.call(callback)!=="[object Function]")
throw new TypeError( callback + " is not a function" );
if (thisArg)
T = thisArg;
k = 0;
while(k<len) {
var kValue;
if (Object.prototype.hasOwnProperty.call(O, k)) {
kValue = O[k];
callback.call( T, kValue, k, O );
}
k++;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment