javascript - 无法将对象推入数组,因为它是未定义的 Javascript
问题描述
我正在尝试将 Card 类型的对象推送到数组中,但我得到:
无法读取未定义的属性“推送”。
这是班级卡片:
class Card {
constructor(value, suit) {
this.Value = value;
this.Suit = suit;
this.Viewed = false;
}
}
这是班级游戏:<br/ >
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);
,它会将 undefined 打印到控制台中,然后给我一个错误。
有人能告诉我为什么首先未定义 GroundCards 属性吗?
我还尝试在构造函数中对其进行初始化,例如:
this.GroundCards = new Array();
它仍然无法正常工作。
我对 Javascript 语法还是有点陌生,对于任何明显的错误,我深表歉意。
谢谢你的时间。
解决方案
您的构造函数被命名construct
- 因此它永远不会被调用。应该叫它constructor
。
推荐阅读
- java - 在本地主机上使用 Spring Boot 应用程序出现 404 错误,日志中没有异常
- ruby-on-rails - 将 Rails 模式转储为 SQL 时,用户“root”的密码身份验证失败
- java - 单元测试 HttpURL 连接
- f# - F#查询表达式:如何进行左连接并返回连接表为空的那些记录?
- selenium-webdriver - 如何处理随机发生的 staleElementException。在我的情况下,它随机抛出异常
- jquery - jquery 目标组
一组元素之间
- java - 滑动以关闭更改底页中箭头的位置
- docker - 在多步构建中复制时 Docker 构建失败
- angular - 如何将对象列表复制到另一个 [Typescript]
- amazon-web-services - 在我将文件夹上传到 AWS Glacier 时压缩它们