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

133
Visualizações
Equating Variable with Object Method

I was working through a JS newsletter which had a little quiz for what would be the output of the following program:

window.name = "Mike Tyson";

const me = {
    name: "Tyler",
    sayName(): {
        console.log(this.name);
    },
};

const sayName = me.sayName;
sayName();

Why does this print "Mike Tyson" instead of "Tyler"? When I set a variable (sayName) equal to an object method, I had thought the variable would be referencing the name variable inside the object it is defined in since it would close over its lexical scope even when called outside it (can confirm me.sayName() does print "Tyler").

My understanding of variables referencing objects & functions must be incorrect

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