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里面做的事


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM