我收到此错误,但无法弄清楚原因:
this._courses[courseName].push(dish); ^
类型错误:无法在 Object.addDishToCourse 读取未定义的属性“推送”
let menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
get appetizers() {
return this.appertizers;
},
set appetizers(newAppetizer) {
this.appetizers = newAppetizer;
},
get mains() {
return this.mains
},
set mains(newMain) {
this.mains = newMain;
},
get desserts() {
return this.desserts;
},
set desserts(newDessert) {
this.desserts = newDessert;
},
},
get courses() {
return {
appetizers: this._courses.appetizers,
mains: this._courses.mains,
desserts: this._courses.desserts
};
},
addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
};
this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndedx];
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse("appetizers");
const main = this.getRandomDishFromCourse("mains");
const dessert = this.getRandomDishFromCourse("desserts");
const totalPrice = appetizer.price + main.price + dessert.price;
return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. Your total is $${totalPrice}.`
}
}
慕娘9325324
GCT1015
相关分类