如何修复语法错误:JavaScript、Firefox 中当前不支持字段错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56083707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:35:32  来源:igfitidea点击:

How to fix SyntaxError: fields are not currently supported error in JavaScript, Firefox?

javascriptfirefox

提问by Praveen Kumar Purushothaman

I've just been learning JavaScript, and I'm fairly new to coding. I was learning about properties and fields and for some reason Firefox won't run if my class contains fields.

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

Here's some code that gives the error in the title.

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

class Animal {
  _paws = true;

  get paws() {
    return _paws;
  }
}

let cat = new Animal();

console.log(cat.paws);

回答by Praveen Kumar Purushothaman

Firstly, there's an error in your code. It should be this._pawsin the returnstatement.

首先,您的代码中存在错误。它应该this._pawsreturn声明中。

class Animal {
  _paws = true;

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

let cat = new Animal();

console.log(cat.paws);

And coming to browser compatibility, if you check Public class fields - Chrome Platform Status, you can clearly see that Firefox is still under development.

关于浏览器兼容性,如果您检查Public class fields - Chrome Platform Status,您可以清楚地看到 Firefox 仍在开发中。

status

地位

From Field declarations...

字段声明...

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.

公共和私有字段声明是JavaScript 标准委员会TC39提出的一项实验性功能(第 3 阶段)。浏览器中的支持是有限的,但是可以通过Babel 等系统的构建步骤使用该功能。