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);  //报错

posted on 2021-08-10 22:46  In-6026  阅读(109)  评论(0)    收藏  举报

导航