在类里面添加静态方法可以使用staitc这个关键字,staitc就是不需要实例化类就可以使用的方法

 

class Chef{
  constructor(food){
    this.food = food;
    thid.dish = [];
  }

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

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

  staitc cook(food){
    console.log(this.food)
  }
}
Chef.cook('tomato') //tomato

 

cook是个静态方法,不需要实例化就可以直接使用,这里得到的结果就是cook里面做的事

相关文章:

  • 2021-09-08
  • 2021-10-22
  • 2022-12-23
  • 2021-05-31
  • 2022-01-21
  • 2022-01-24
  • 2022-12-23
  • 2021-06-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-01-26
  • 2021-07-06
  • 2022-02-09
相关资源
相似解决方案