Always rely on Array.forEach

After finishing a small JavaScript application tested on Chrome, I realized that Internet Explorer doesn’t support the Array.forEach function. What a surprise IE people.
So I found this on the mozilla website, works like a charm!

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

Leave a Reply

Your email address will not be published. Required fields are marked *