[英]JavaScript TypeError, Cannot read property of undefined, but the function is defined. What am I doing wrong?
I keep getting the error: "TypeError: Cannot read property 'push' of undefined" 我不断收到错误消息:“ TypeError:无法读取未定义的属性'push'”
The error is in this line: "this.courseName.push(dish);" 错误在以下行中:“ this.courseName.push(dish);”
Here is the code: 这是代码:
let menu = {
_courses: {
_appetizers:[] ,
_mains:[],
_desserts: []
},
addDishToCourse(courseName, dishName, dishPrice){
const dish = { name: this.dishName, price: this.dishPrice };
// ERROR HERE: **this._courses[courseName].push(dish);**
},
};
I have been stuck with this error for hours. 我已经被这个错误困扰了几个小时。 Is there something I'm not seeing?
有没有我看不到的东西吗?
Sorry if there is a simple solution to this. 抱歉,如果有一个简单的解决方案。 I'm relatively new to JavaScript.
我对JavaScript比较陌生。
Edit: 编辑:
I was indeed calling the key incorrectly. 我确实是错误地调用了密钥。 I am now calling the function as:
我现在将该函数称为:
menu.addDishToCourse("_appetizers", "random", 10);
console.log(menu._courses._appetizers);
When I log this in compiler it returns: 当我在编译器中登录时,它返回:
[ { name: undefined, price: undefined } ]
It seems like the function isn't getting called. 似乎没有调用该函数。 Am I approaching this fundamentally incorrectly?
我是否从根本上错误地解决了这个问题?
It is the scope
that is the issue. 问题在于
scope
。
this
is the object
itself (the menu in this case) and has _courses
and addDishToCourse
as its properties. this
是object
本身(在这种情况下为菜单),并具有_courses
和addDishToCourse
作为其属性。
Inside the addDishToCourse
this
is looking at the object
for the dishName
and dishPrice
, but they are scoped to the function itself (from the parameters). 在
addDishToCourse
内部, this
是在查看object
的dishName
和dishPrice
,但它们的作用域dishPrice
于函数本身(通过参数)。
So the correct code would be like so: 因此正确的代码如下所示:
let menu = {
_courses: {
_appetizers: [],
_mains: [],
_desserts:[]
},
addDishToCourse(courseName, dishName, dishPrice) {
const dish = { name: dishName, price: dishPrice };
this._courses[courseName].push(dish);
}
}
menu.addDish("_mains", "Pizza", "10");
Your first problem is that you were passing a wrong courseName
to the function, that's why you get the error. 您的第一个问题是,您向函数传递了错误的
courseName
,这就是为什么会出现错误。
And your second problem is that you are assigning wrong values, to object to push in your courses array when you used { name: this.dishName, price: this.dishPrice }
. 第二个问题是使用
{ name: this.dishName, price: this.dishPrice }
时,您分配了错误的值来反对将您的课程数组推入。
In fact when you use this.dishName
you were trying to get dishName
from your menu
object that's why it's undefined
, you just need to use the parameters dishName
and dishPrice
you are passing arguments to the function. 实际上,当您使用
this.dishName
您试图从menu
对象中获取dishName
,这就是undefined
的原因,您只需要使用参数dishName
和dishPrice
dishName
, dishPrice
可以将参数传递给该函数。
const dish = {
name: dishName,
price: dishPrice
};
this._courses[courseName].push(dish);
This is a working Demo: 这是一个有效的演示:
let menu = { _courses: { _appetizers: [], _mains: [], _desserts: [] }, addDishToCourse(courseName, dishName, dishPrice) { const dish = { name: dishName, price: dishPrice }; this._courses[courseName].push(dish); }, }; menu.addDishToCourse("_appetizers", "random", 10); console.log(menu._courses._appetizers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.