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

68
Views
destructuring losing getter definition

Simplified version of my code. I define getter-setter for var1, then I destructure it to child.

I expect child to get the definition of var1 but it does not. I expect the last line to print 40 40 when I set child.var1 but it prints 30 40 instead. If I use Object.assign the parent will get var2.

How do I retain the getter-setter of var1 on child but also I want the parent not to get var2?

let val = 10;

let parent = {
  set var1(_val) {
    val = _val;
  },
  get var1() {
    return val;
  }
}

console.log(parent.var1); // 10
parent.var1 = 20;
console.log(parent.var1); // 20

let child = { ...parent, var2: 1 };
parent.var1 = 30;
child.var1 = 40;
console.log(parent.var1, child.var1); // 30 40

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!