Skip to content

Instantly share code, notes, and snippets.

@EarMaster
Created December 19, 2012 10:01

Revisions

  1. EarMaster created this gist Dec 19, 2012.
    27 changes: 27 additions & 0 deletions forEach.polyfill.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * 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++;
    }
    };
    }