IE11 在 javascript 中定义类时出现 SCRIPT1002 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37194202/
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
IE11 gives SCRIPT1002 error when defining class in javascript
提问by REJH
I have some trouble with IE11 and a static javascript class I wrote.
我在使用 IE11 和我编写的静态 javascript 类时遇到了一些麻烦。
The error I get is:
我得到的错误是:
SCRIPT1002: Syntax error rgmui.box.js (6,1)
SCRIPT1002:语法错误 rgmui.box.js (6,1)
Which points to:
其中指出:
// ===========================================
// RGMUI BOX
// Static class
class RgMuiBox {
^
So I'm guessing I'm defining this class in the wrong way? What's the correct way of doing this?
所以我猜我以错误的方式定义了这个类?这样做的正确方法是什么?
I found a post on SO that seems to point out that the issue is ES5 vs ES6 - and I figure IE11 doesn't support ES6?
我在 SO 上找到了一篇帖子,似乎指出问题是 ES5 与 ES6 - 我认为 IE11 不支持 ES6?
Just to be complete, this is what I have (simplified):
为了完整起见,这就是我所拥有的(简化):
class RgMuiBox {
static method1() {
// .. code ..
}
}
Thanks!
谢谢!
回答by user8576017
Hate to reopen such an old issue, but it still shows up high in the results, so I'll add what I found out:
讨厌重新打开这样一个老问题,但它仍然出现在结果中,所以我将添加我发现的内容:
To reiterate what @Mikey and @REJH said, classes are not recognized by IE11.
重申@Mikey 和@REJH 所说的,IE11 不识别类。
That said, tools like Babelwill allow you to translate classes into something that will run on IE11.
也就是说,像Babel这样的工具可以让你将类转换成可以在 IE11 上运行的东西。
回答by REJH
@Mikey is right. IE11 does not recognize this syntax for classes because ES6 spec: https://kangax.github.io/compat-table/es6/
@Mikey 是对的。IE11 无法识别类的此语法,因为 ES6 规范:https://kangax.github.io/compat-table/es6/
class RgMuiBox {
static method1() {
// .. code ..
}
}
I'm still not sure if the following is the correct way to define a static class but it works:
我仍然不确定以下是否是定义静态类的正确方法,但它有效:
var RgMuiBox = {};
RgMuiBox.method = function() {
// ....
}
Just putting it out here so this question has some sort of an answer that might help people get going. If there are alternatives to the above I like to hear about those!
只是把它放在这里,所以这个问题有某种答案可以帮助人们开始。如果有上述替代方案,我喜欢听听那些!
回答by Negi Rox
Static class Example
静态类示例
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;
})();