I'm diving into Javascript callbacks and stuff. I came around the forEach() function. The functioname says it all, loop through each object in a list.
When I look into the documentation I see the following syntax:
arr.forEach(callback[, thisArg])
And the documentation also mentions the parameters
currentValue
index
array
I stumbled upon an easy example of a forEach implementation:
var friends = ["Mike", "Stacy", "Andy", "Rick"];
friends.forEach(function (eachName, index){
console.log(index + 1 + ". " + eachName);
});
And this gives me the obvious output:
"1. Mike"
"2. Stacy"
"3. Andy"
"4. Rick"
I can live with the behaviour about everything and the result it gives, but I want to know why this is working and I'm confused about this
...function (eachName, index)...
When and where or how are eachName and index being filled with the correct value? Or how can I see how forEach is implemented, cause I'm guessing that this one is doing the magic here? Or am I missing an important concept here?
Array.prototype.forEach(), or in the annotated ES5