15.4.4.18 Array.prototype.foreach(callbackfn[, thisArg])

2010-07-05

void Array.prototype.foreach(callbackfn:function[, thisArg:mixed=undefined])

Call callbackfn once for every element in the array.

The callback callbackfn should match this fingerprint: void|mixed function(element:mixed, index:int, object:object).

The this value will be coerced into an object, if thisArg was supplied it is not changed and it is undefined otherwise.

The forEach method does not change the array it works on.

The range of forEach is determined before actually calling the callbackfn the first time. The value passed on is the value of the array at the time of calling callbackfn (so if callbackfn changes it, the new value is supplied). Deleted elements are skipped. Elements added beyond the initial length of the array during by callbackfn are not processed.

Array.prototype.forEach.length = 1

Code: (Meta Ecma)
Array.prototype.forEach = function(callbackfn,thisArg){
var O = ToObject(this);
var lenValue = O.[[Get]]("length");
var len = ToUint32(lenValue);
if (!IsCallable(callbackfn)) throw TypeError;
if (arguments.length >= 2) var T = this.arg;
else var T = undefined; // actually, this line is not required ;)
var k = 0;
while (k < len) {
var Pk = ToString(k);
var kPresent = O.[[HasProperty]](Pk);
if (kPresent) {
var kValue = O.[[Get]](Pk);
callbackfn.[[Call]](T, [kValue, k, O]);
}
++k;
}
}