IE11在javascript中定义类时给出SCRIPT1002错误

IE11 gives SCRIPT1002 error when defining class in javascript

本文关键字:SCRIPT1002 错误 javascript 定义 IE11      更新时间:2023-09-26

我在IE11和我编写的一个静态javascript类方面遇到了一些问题。

我得到的错误是:

SCRIPT1002:语法错误rgmui.box.js(6.1)

哪个指向:

// ===========================================
// RGMUI BOX
// Static class
class RgMuiBox {
^

所以我猜我对这个类的定义是错误的?正确的做法是什么?

我在SO上发现了一个帖子,似乎指出问题是ES5与ES6——我认为IE11不支持ES6?

为了完整起见,这就是我所拥有的(简化):

class RgMuiBox {
    static method1() {
    // .. code ..
    }
}

谢谢!

我不想重新打开这样一个旧问题,但它仍然在结果中显示得很高,所以我将添加我发现的内容:

重申一下@Mikey和@REJH所说的,IE11不承认类。

也就是说,像Babel这样的工具将允许你将类翻译成在IE11上运行的东西。

@Mikey是对的。IE11无法识别类的此语法,因为ES6规范:https://kangax.github.io/compat-table/es6/

class RgMuiBox {
    static method1() {
    // .. code ..
    }
}

我仍然不确定以下是否是定义静态类的正确方法,但它有效:

var RgMuiBox = {};
  RgMuiBox.method = function() {
    // ....
  }

把它放在这里,这样这个问题就有了某种答案,可能会帮助人们继续前进。如果有以上的替代方案,我很想听听!

静态类示例

var _createClass = (function () {
    function defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
            var descriptor = props[i];
            descriptor.enumerable = descriptor.enumerable || false;
            descriptor.configurable = true;
            if ("value" in descriptor) descriptor.writable = true;
            Object.defineProperty(target, descriptor.key, descriptor);
        }
    }
    return function (Constructor, protoProps, staticProps) {
        if (protoProps) defineProperties(Constructor.prototype, protoProps);
        if (staticProps) defineProperties(Constructor, staticProps);
        return Constructor;
    };
})();
function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
var StaticClass = (function () {
    function StaticClass() {
        _classCallCheck(this, StaticClass);
    }
    _createClass(StaticClass, null, [{
        key: "method1",
        value: function method1() {
            // .. code ..
        }
    }]);
    return StaticClass;
 })();