In Javascript, with the following illustration code:
class Base {
constructor() { this._val = 1 }
get val() { return this._val }
}
class Xtnd extends Base {
set val(v) { this._val = v }
}
let x = new Xtnd();
x.val = 5;
console.log(x.val); // prints 'undefined'
the instance x
will not inherit get val()...
from Base
class. As it is, Javascript treat the absence of a getter, in the presence of the setter, as undefined.
I have a situation in which I have many classes that all have the exact same set of getters but unique setters. Currently, I simply replicate the getters in each class, but I'm refactoring and want to eliminate the redundant code.
Is there a way to tell JS to keep the getter from the base class, or does anyone have an elegant solution to this problem?