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