ES6基礎之——get 與 set


在類里面可以去定義一些getter和setter,getter可以得到一些東西的方法,setter可以設置東西
class Chef{
  constructor(food){
    this.food = food;
    thid.dish = [];
  }

  //getter
  get menu(){
    return this.dish
  }

  //setter
  set menu(dish){
    this.dish.push(dish)
  }

  cook(){
    console.log(this.food)
  }
}

let zhangsan =new Chef();
console.log(zhangsan.menu = 'tomato' ); //tomato
console.log(zhangsan.menu = 'pizza' ); //pizza
console.log(zhangsan.menu); //["tomato","pizza"]

  


免責聲明!

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



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