Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!