ES6基础之——静态方法staitc

在类里面添加静态方法可使用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里面作的事
相关文章
相关标签/搜索