在类里面可以去定义一些getter和setter,getter可以得到一些东西的方法,setter可以设置东西
class Chef{
  constructor(food){
    this.food = food;
    thid.dish = [];
  }

  //getter
  get menu(){
    return this.dish
  }

  //setter
  set menu(dish){
    this.dish.push(dish)
  }

  cook(){
    console.log(this.food)
  }
}

let zhangsan =new Chef();
console.log(zhangsan.menu = 'tomato' ); //tomato
console.log(zhangsan.menu = 'pizza' ); //pizza
console.log(zhangsan.menu); //["tomato","pizza"]

  

相关文章:

  • 2021-10-07
  • 2021-04-26
  • 2021-05-20
  • 2022-12-23
  • 2021-09-09
  • 2022-02-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案