In a current JavaScript project where ES6 class syntax and get/set syntax are used I stumbled upon a behaviour I cannot explain.
First, an extracted demo that works as expected:
class A {
constructor() {
this.__value = null;
}
get value() {
return this.__value;
}
set value(value) {
this.__value = value;
}
}
class B extends A { }
let b = new B();
b.value = 2;
console.log(b.value); // output: 2
Setting and getting b.value (defined in A.prototype) works.
Now consider the following demo in which I moved just the setter from A to B:
class A {
constructor() {
this.__value = null;
}
get value() {
return this.__value;
}
}
class B extends A {
set value(value) {
this.__value = value;
}
}
let b = new B();
b.value = 2; // b.__value is 2
console.log(b.value); // output: undefined
Setting value works (since b.__value is indeed 2), but the getter does not seem to exist although it is still defined in A.prototype.
What is the catch here?