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
Class B extends A {
constructor(x) {
super(x);
this.X = x;
}
get X() { return this.X }
set X(x) { this.X = x }
}