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

138
Visualizações
Why is the 'this' value different in Javascript?
var obj = {
    a: 2,
}
var a = 3;

const func = () => {
    console.log(this.a);
}

function test() {
    setTimeout(func, 100);
}

function test2() {
    setTimeout(() => {
        console.log(this.a);
    }, 100);
}
test.call(obj); //3
test2.call(obj); //2

It seemed that the test and test2 are the same, but the returned result is different, what's the problem with 'this'?

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

0

with test arrow function, this will refer to the window object, and as var a = 3, the a will be assigned as a property to the window, it will be like

window.a

But in test2 this will refer to the obj object.

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