【问题标题】:Why does Firefox give a syntax error, class is a reserved identifier?为什么 Firefox 给出语法错误,class is a reserved identifier?
【发布时间】:2016-03-28 07:25:46
【问题描述】:

在 Firefox 43 上使用以下代码打开名为 index.html 的文件会出现以下错误:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title></title>
    <script>
    "use strict";
    class RangeIterator {}
    </script>
    </head>
    <body>
    </body>
    </html>

我在控制台中看到以下错误:

SyntaxError: class is a reserved identifier

知道为什么会出现这个错误吗?

【问题讨论】:

标签: javascript html ecmascript-6


【解决方案1】:

根据Can I Use ES6 classes 支持form 45 版本的Firefox。如果 45 版本也抛出类似 SyntaxError: class is a reserved identifier 的错误,那么最好转换为 ES5。


Babel 是一个 JavaScript 编译器。使用它将ES6 转换为ES5 格式BABEL JS(或)ES6Console

ES2015 classes « 检查下面的Converted 代码,从ES6ES5

// ES6                              ES5
class Shape {                   "use strict";
  constructor(color) {
    this._color = color;        function _classCallCheck(instance, Constructor) {
                                  if (!(instance instanceof Constructor)) {
                                    throw new TypeError("Cannot call a class as a function");
  }                               }
}                               }
                                var Shape = function Shape(color) {
                                  _classCallCheck(this, Shape);
                                  this._color = color;
                                };

@见

【讨论】:

    【解决方案2】:

    根据this,Firefox 版本

    【讨论】:

    • 这很简单。谢谢。
    猜你喜欢
    • 2015-10-30
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2016-06-27
    相关资源
    最近更新 更多