ES11——class类的私有属性
通过 # 申明
class girl {
//共有属性
name;
//私有属性
#age;
#weight;
constructor(name, age, weight) {
this.name = name;
this.#age = age;
this.#weight = weight;
}
introduce() {
console.log('自我介绍:', this.#age, this.#weight);
}
}
const LLC = new girl('LLC', 18, '45kg');
LLC.introduce(); //可以
console.log("外部获取:", LLC.#age, LLC.#weight); //报错
浙公网安备 33010602011771号