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裏面作的事
相關文章
相關標籤/搜索