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

242
Visualizações
Arrow functions in object literal vs arrow functions in class generated objects
const objectLiteral = {
  x: 5,
  getX: () => {
      console.log(`The value of x is ${this.x}`); 
  }
} 
x.getX(); // The value of x is undefined

Why this doesn't bind to the getX method when I use an object literal? Doesn't the arrow function get their this from the parent scope? In this case the objectLiteral. But it does when I use class generated object.

class classObject {
  x = 5;
  getX = () => {
      console.log(`The value of x is ${this.x}`);
  }

  const classObjectInstance = new classObject();
  classObjectInstance.getX(); // The value of x is 5
}
about 4 years ago · Juan Pablo Isaza
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