JavaScript错误:无法读取未定义的属性“ push”

时间:2019-08-05 19:16:02

标签: javascript

我收到此错误,但我不知道为什么:

  

this._courses [courseName] .push(dish);                                ^

     

TypeError:无法读取未定义的属性“ push”       在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}.`
        }
    
    }
    
    
    menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25);
    menu.addDishToCourse('appetizers', 'Prawn Coctail', 4.25);
    menu.addDishToCourse('appetizers', 'Garlic Bread', 3.50);
    
    menu.addDishToCourse('mains', 'Lasagna', 9.75);
    menu.addDishToCourse('mains', 'Ribeye Steak', 14.95);
    menu.addDishToCourse('mains', 'Fish & Chips', 12.95);
    
    menu.addDishToCourse('desserts', 'Cheese Cake', 4.50);
    menu.addDishToCourse('desserts', 'Creme Brule', 4.25);
    menu.addDishToCourse('desserts', 'Cheese Board', 3.25);
   
let meal = menu.generateRandomMeal();

console.log(meal);

4 个答案:

答案 0 :(得分:1)

您不能有一个属性获取器和一个名称完全相同的属性。一个优先于另一个。但是,由于您正在实现默认行为,所以这里实际上并没有指向属性获取器和设置器。

如果您将_courses声明为:

    _courses: {
        appetizers: [],
        mains: [],
        desserts: [],
    }

不要使用getter方法,它应该可以按预期工作。


如果您确实想在此处使用属性设置器和获取器,则必须为存储值的基础属性赋予与获取器/设置器不同的名称。

答案 1 :(得分:0)

尝试这种方式:

let menu = {
        _courses: {
            _appetizers: [],
            _mains: [],
            _desserts: [],
            get appetizers() {
                return this._appetizers;
            },
            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[randomIndex];
        },
        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}.`
        }
    }

    menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25);
    menu.addDishToCourse('appetizers', 'Prawn Coctail', 4.25);
    menu.addDishToCourse('appetizers', 'Garlic Bread', 3.50);

    menu.addDishToCourse('mains', 'Lasagna', 9.75);
    menu.addDishToCourse('mains', 'Ribeye Steak', 14.95);
    menu.addDishToCourse('mains', 'Fish & Chips', 12.95);

    menu.addDishToCourse('desserts', 'Cheese Cake', 4.50);
    menu.addDishToCourse('desserts', 'Creme Brule', 4.25);
    menu.addDishToCourse('desserts', 'Cheese Board', 3.25);

    let meal = menu.generateRandomMeal();

    console.log(meal);

答案 2 :(得分:0)

您的代码中有错字:

get appetizers() {
    return this.appertizers; // <-- Typo: should be appetizers
}

此外,如果仍然有问题,请确保获得预期的结果。例如,尝试运行控制台日志以查看是否产生了预期的结果。

console.log(this._courses[courseName]);

答案 3 :(得分:-1)

问题是您在菜单中定义了_courses。相反,this._courses指向菜单中不存在的变量。 您应该引用菜单而不是菜单,因为菜单的范围不是功能。您还应该在json中更改所有其他引用

this.menu = {
    _courses: {
        ...
    },
    ... 
    addDishToCourse(courseName, dishName, dishPrice) {
        const dish = {
            name: dishName,
            price: dishPrice,
        };

        menu._courses[courseName].push(dish);
    }
}