Array.prototype.forEach = function(callback, context) {
for (var i = 0; i < this.length; i++) {
callback.call(context || null, this[i], i, this);
}
};
["a", "b", "c"].forEach(function(value, index, array) {
assert(value,
"Is in position " + index + " out of " +
(array.length - 1));
});
I don't fully understand why null
is used here. I guess when I use the invoke foreach
, if I miss the context
parameter, it will replace it with null
? Will callback.call(context || null, this[i], i, this)
execute? Can someone explain this for me?