ES6 basis of - Static method staitc

Add a static method can use this keyword in class staitc inside, staitc that do not need to instantiate a class you can use the method

 

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 is a static method, does not need to instantiate can be used directly, the results obtained here to do is cook inside

Guess you like

Origin www.cnblogs.com/fe-cherrydlh/p/11130207.html