Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
in JS I Don't Know this Expression
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](); < -- - what is this ?

army[5](); < -- - what is this ?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Let's do this back-to-front:

let army = makeArmy();

That calls a function...

// ...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]();

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda