javascript forEach

by kazu69

HTML

の

JavaScript

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    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);
    }
  };
}

function printElt(element, index, array) {
    document.write("arrayのindex :[" + index + "]  要素は " + element + '<br>'); // print は定義済みとする
}
[1, 2, 3].forEach(printElt);