Created
December 19, 2012 10:01
-
-
Save EarMaster/4335676 to your computer and use it in GitHub Desktop.
polyfill for forEach array iteration function
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
/** | |
* 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