read the MDN documentation and found no corresponding instructions, but when I actually tested it, I found that _ under the getter, setter of class is not just a naming convention, but there are really special rules. For example:
class Test{
constructor(){
this.a = 1;
}
get a(){
return this._a;
}
set a(value){
this._a = value;
}
}
const t = new Test();
**console.log("t.a", t.a); //1**
class Test2{
constructor(){
this.a = 1;
}
get b(){
return this._a;
}
set b(value){
this._a = value;
}
}
const t2 = new Test2();
**console.log("t2.b", t2.b); //undefined**
getter named a can get the value of this.a
through this._a
. If getter
is changed to return this.a;
, it will loop infinitely. So it is necessary to use return this._a;
, but there is no description in the document that class getter must use _.
getter named b cannot get this._a
excuse me, where can I write such a rule? Why is there such a design?