If you put _ in the setter or getter before the name of the method / property, what will the interpreter do?
Nothing will do.
In this case, the following javascript feature is used: creating a field in its absence at the time of assignment.
The first time this._name assigned, an appropriate field is created in the object.
As a result, the record of this class is equivalent to the following:
class Ab{ constructor(n){ this._name = n; } get name(){return this._name } set name(value){ this._name = value } }
The behavior has not changed in any way, but now it is clear that the get and set methods simply use the existing field, which is initialized in the constructor.
class Ab { constructor(n) { this.name = n; } get name() { return this.$name } set name(value) { this.$name = value } } console.log(new Ab(1).name); class Ab1 { constructor(n) { this.name = n; } get name() { return this.custom } set name(value) { this.custom = value } } console.log(new Ab1(2).name);