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

240
Visualizações
What's the difference between two set of codes, I know what arrow function does but why traditional expression does not work?

What's the difference between two set of codes, I know what arrow function does but why traditional expression does not work?

Set - 1

loop through all elements

    checkboxes.forEach( function (checkbox){
        console.log(checkbox);

        if( checkbox === this || checkbox === lastChecked)
        {
            inBetween = !inBetween;
            console.log(" start checking them inbetween");

        }

        if(inBetween)
        {
            checkbox.checked = true;
        }
    });

Set - 2

loop through all elements

  checkboxes.forEach(checkbox => {

  console.log(checkbox);
  if (checkbox === this || checkbox === lastChecked) {
    inBetween = !inBetween;
    console.log('Starting to check them in between!');
  }

  if (inBetween) {
    checkbox.checked = true;
  }
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Set - 1 is foreach with ES5 definition style whereas Set-2 is for ES6. Make sure the target/version supports ES5 so that Set-1 will work.

Also, you might wanna try replacing this with self in set-1 and defining self as this before the foreach or use bind

  var self = this;
this.addNewObjects = function(arr){
    arr.forEach(function(obj) {
        self.addObject(new Obj(obj.prop1, obj.prop2));
    });
}

or

    this.addNewObjects = function(arr) {
    arr.forEach(function(obj) {
        this.addObject(new Obj(obj.prop1, obj.prop2));
    }.bind(this));
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think, may be u use this in scope of forEach() function, this is one of more different of two.
more explain is: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

about 4 years ago · Juan Pablo Isaza 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