gpt4 book ai didi

javascript - 如何修复 SyntaxError : fields are not currently supported error in JavaScript, Firefox?

转载 作者:行者123 更新时间:2023-12-01 06:25:22 27 4
gpt4 key购买 nike

我刚刚学习 JavaScript,而且我对编码还很陌生。我正在学习属性和字段,由于某种原因,如果我的类包含字段,Firefox 将无法运行。

这是一些在标题中给出错误的代码。

class Animal {
_paws = true;

get paws() {
return _paws;
}
}

let cat = new Animal();

console.log(cat.paws);

最佳答案

首先,您的代码中存在错误。应该是 this._pawsreturn陈述。

class Animal {
_paws = true;

get paws() {
return this._paws; // Correct here.
}
}

let cat = new Animal();

console.log(cat.paws);

自发布此答案以来,Firefox 已获得对公共(public)(和私有(private))类字段声明的支持。 Chrome Platform Status这方面的文档已经过时了,从现在的 FIXED 可以看出 Mozilla bug 的状态它链接到。
相当于 Mozilla documentation显示从版本 69 开始支持公共(public)字段声明。顺便说一下, 这个图表也部分过时,事实上 Firefox Android has the same level of support as Firefox Desktop .
对于此功能的生产使用,您应该牢记 Mozilla 文档中的以下警告:

Public and private field declarations are an experimental feature (stage 3) proposed at TC39, the JavaScript standards committee. Support in browsers is limited, but the feature can be used through a build step with systems like Babel.


上面引用的两个开发者资源网站都表明 Safari 仍然缺乏对这个功能的支持(尽管考虑到它们的不完整,谁能说这是否仍然准确)。

关于javascript - 如何修复 SyntaxError : fields are not currently supported error in JavaScript, Firefox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com