美文网首页
类中的 set get

类中的 set get

作者: 基本密码宋 | 来源:发表于2017-10-31 17:51 被阅读10次
    class Chef {
    
        constructor(food) {
            this.food = food;
            this.data;
        }
    
        cook() {
            console.log('我在做饭' + this.food);
        }
    
        set menu(data) {   //set方法
            this.data = data;
        }
    
        get menu() {     //get方法
            return this.data;
        }
    
    }
    
    
    let c = new Chef('米饭');
    c.cook();
    c.menu = 500;
    console.log(c.menu)
    
    

    相关文章

      网友评论

          本文标题:类中的 set get

          本文链接:https://www.haomeiwen.com/subject/trabpxtx.html