无法将对象推入数组,因为它未定义 Javascript
Can't push objects into array because it's undefined Javascript
我正在尝试将 Card 类型的对象推入数组,我得到:
无法读取未定义的 属性 'push'。
这是 class 卡片:
class Card {
constructor(value, suit) {
this.Value = value;
this.Suit = suit;
this.Viewed = false;
}
}
这是 class 游戏:
class Game{
construct(){
this.GroundCards = []; //This is the array i am trying to push objects at.
this.GroundCard = null;
this.NbCardsPickedSeven = 0;
}
pushCard(card){
console.log(this.GroundCards);
this.GroundCards.push(card);
}
}
当我这样做时 console.log(this.GroundCards);
它在控制台中打印未定义,然后它给我一个错误。
有人能告诉我为什么 GroundCards 属性首先未定义吗?
我还尝试在构造函数中初始化它,例如:
this.GroundCards = new Array();
还是不行。
我对 Javascript 语法还是有点陌生,对于任何明显的错误,我们深表歉意。
谢谢你的时间。
您的构造函数名为 construct
- 因此它永远不会被调用。应该叫constructor
.
我正在尝试将 Card 类型的对象推入数组,我得到:
无法读取未定义的 属性 'push'。
这是 class 卡片:
class Card {
constructor(value, suit) {
this.Value = value;
this.Suit = suit;
this.Viewed = false;
}
}
这是 class 游戏:
class Game{
construct(){
this.GroundCards = []; //This is the array i am trying to push objects at.
this.GroundCard = null;
this.NbCardsPickedSeven = 0;
}
pushCard(card){
console.log(this.GroundCards);
this.GroundCards.push(card);
}
}
当我这样做时 console.log(this.GroundCards);
它在控制台中打印未定义,然后它给我一个错误。
有人能告诉我为什么 GroundCards 属性首先未定义吗?
我还尝试在构造函数中初始化它,例如:
this.GroundCards = new Array();
还是不行。
我对 Javascript 语法还是有点陌生,对于任何明显的错误,我们深表歉意。
谢谢你的时间。
您的构造函数名为 construct
- 因此它永远不会被调用。应该叫constructor
.