类中的 set get

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)