JS ES6 클래스를 이해하려고하는데 "최대 호출 스택 크기가 초과되었습니다."라는 메시지가 나타나면 "this"로 대답합니다. 변수. 이 예제를 살펴 보겠습니다.ES6 JS 클래스 밑줄 집합 및 get 메서드 반환 "최대 호출 스택 크기 초과"
class Human {
constructor(age) {
this.age = age;
// "this._age = age;" output:
// Property age of instance without underscore: 34
// Property age of instance with underscore: 34
}
get age() {
return this._age;
// Without underscore error: "Uncaught RangeError: Maximum call stack size exceeded"
}
set age(age) {
this._age = age;
// Without underscore error: "Uncaught RangeError: Maximum call stack size exceeded"
console.log(`Without underscore: ${this.age}`);
console.log(`With underscore: ${this._age}`);
}
}
let john = new Human(34);
console.log(`Property age of instance without underscore: ${john.age}`);
console.log(`Property age of instance with underscore: ${john._age}`);
get 및 set 메서드에서 그 밑줄을 사용해야하는 이유는 무엇입니까? 왜 출력물이 생성자에서 사용할 때와 같이 변하는가? 인스턴스 속성을 사용하려고 할 때 밑줄을 사용하거나 사용하지 않는 미터가없는 이유는 무엇입니까? mdn 문서에는 밑줄이 전혀 없습니다.