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

102
Views
Change `this` reference to parent object in JS class constructor

I have a class constructor with a nested object structure. Each level has a property x.

this.x > this.a.x > this.a.b.x

I want this.a.b.x to point to the value of this.a.x, but it instead points to this.x.

class Example {
    constructor() {
        this.x = 0;
        this.a = {
            x: this.x + 5,
            b: {
                x: this.x // I want this to equal 5
            }
        }
    }
}

const ex = new Example();
console.log(ex.x) // 0
console.log(ex.a.x) // 5
console.log(ex.a.b.x) // 0 <-- I want this to be 5

How can I change the this reference in this.a.b to point to this.a rather than this??

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!