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

234
Visualizações
How exactly does javascript set context for objects created from constructor function using the new keyword

Consider the following constructor function having a method logging the context

function Foo() {
  this.logContext1 = () => console.log('logContext1', this);
}

let foo = new Foo();

foo.logContext2 = () => console.log('logContext2', this);

foo.logContext1();
foo.logContext2();

logContext1 has context (this) set to the Foo object created by new,

whereas logContext2 has context (this) set to the window object

Considering both are arrow functions, why do we see this difference?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

this is defined where the arrow functions are declared:

  • () => console.log('logContext1', this) is declared in the constructor, this is the object being created in that context
  • () => console.log('logContext2', this) is declared on top-level, this is window in that context (or undefined in strict mode)
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