我刚刚学习JavaScript,并且对编码还很陌生。我正在学习属性和字段,由于某种原因,如果我的课程包含字段,Firefox将无法运行。这是一些在标题中给出错误的代码。class Animal { _paws = true; get paws() { return _paws; }}let cat = new Animal();console.log(cat.paws);
2 回答

手掌心
TA贡献1942条经验 获得超3个赞
只需执行以下操作即可快速解决:
class Animal {
// _paws = true; Remove this line.
// Initialize your private fields in constructor instead
constructor() {
this._paws;
}
get paws() {
return this._paws; // Must use this._fieldName to reference
}
}
let cat = new Animal();
console.log(cat.paws);
请注意,除Google Chrome浏览器之外,大多数浏览器上的私有字段仍处于试验阶段。
添加回答
举报
0/150
提交
取消