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

118
Visualizações
Lifetime of a variable inside a function

In the following code, how is it possible that variable temp is still alive after the call to the Decorate ends?

function Decorate(target, key) {
  let temp = target[key];
  Object.defineProperty(target, key, {
    get: function() {
      console.log('getter executed');
      return temp;
    },
    set: function(newValue) {
      console.log('setter executed');
      temp = newValue;
    }
  });
}

class Test {
  x;
  constructor() {}
};

let t = new Test();

Decorate(t, 'x'); //first and only decorator call after which the variable temp should be dead

t.x = 123;  //setter sets x to 123, obviously it somehow still uses temp

t.x = 256;  //second setter call sets x to 256, temp still somehow alive

t.x = 357;  //etc.
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