The map
method provides arguments when calling the callback, which are received by the callback in the a
and i
parameters it declares. (The callback doesn't use a
for anything, but uses i
. The callback also uses str
, which it closes over because the function was created in a context where str
exists.)
It may help to see a rough idea of what map
does internally:
// Note: VERY ROUGH approximation, leaving out all kinds of details (like `thisArg`)
map(callback) {
// Note: `this` is the array you called `map` on
const result = [];
for (let index = 0; index< this.length; ++index) {
result[index] = callback(this[index], index, this);
// In the callback, ^^^^^^^^^^^ ^^^^^
// becomes `a` −−−−−−−−−−−−−−/ \−−−−−−−becomes `i`
}
return result;
}