Skip to content

Instantly share code, notes, and snippets.

@estebistec
Created May 23, 2014 23:09

Revisions

  1. estebistec created this gist May 23, 2014.
    23 changes: 23 additions & 0 deletions Array.prototype.forEach.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill
    if (!Array.prototype.forEach)
    {
    Array.prototype.forEach = function(fun /*, thisArg */)
    {
    "use strict";

    if (this === void 0 || this === null)
    throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
    throw new TypeError();

    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++)
    {
    if (i in t)
    fun.call(thisArg, t[i], i, t);
    }
    };
    }