function makeArmy() { let shooters = []; for (let i = 0; i < 10; i++) { let shooter = function() { alert(i); }; shooters.push(shooter); } return shooters; } let army = makeArmy(); army[0](); <--¿qué es esto?
army[5](); <--¿qué es esto?
Hagamos esto de atrás hacia adelante:
let army = makeArmy();Eso llama a una función...
// ...and the function function makeArmy() { // Creates an array let shooters = []; // It loops from 0 to 9 pushing a new function // into the array on each iteration for (let i = 0; i < 10; i++) { // Create the new function let shooter = function() { // I used console.log because it's much less hassle :) console.log(i); }; // Add the function to the array shooters.push(shooter); } // And then you return the array of functions return shooters; } // An array of functions! let army = makeArmy(); // Call the first function in the array army[0](); // Call the fourth function in the array army[3]();