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

97
Views
Js Inheritance and private members

The following code crashes due to obscure js implementation details.

class A {

  constructor(x) {
    this.X = x
  }

  get X()  { throw new Error('Child should know ...')}
  set X(x) { throw new Error('Child should know ...')}

}

class B 
  extends A {
  #X

  get X()  { return this.#X }
  set X(x) { this.#X = x }
}

const b = new B(3)
  set X(x) { this.#X = x }
                     ^

TypeError: Cannot write private member #X to an object whose class did not declare it
    at B.set X [as X] ([...]/Test.js:17:22)
    at new A ([...]/Test.js:4:12)
    at new B ([...]/Test.js:12:1)
    at Object.<anonymous> ([...]/Test.js:20:11)

Is there a way to keep X private in class B without breaking inheritance logic ?

I hope someone could help me. Regards

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Class B extends A {
 constructor(x) {
   super(x);
   this.X = x;
 }

 get X()  { return this.X }
 set X(x) { this.X = x }
}
about 4 years ago · Juan Pablo Isaza Report
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!