2016-05-12 729 views
3

IE11和我寫的一個靜態JavaScript類有一些問題。在javascript中定義類時,IE11會出現SCRIPT1002錯誤

我得到的錯誤是:

SCRIPT1002:語法錯誤 rgmui.box.js(6,1)

指向:

// =========================================== 
// RGMUI BOX 
// Static class 

class RgMuiBox { 
^ 

所以我猜猜我是以錯誤的方式定義這個班?這樣做的正確方法是什麼?

我在SO上發現了一個帖子,似乎指出問題是ES5與ES6 - 而我圖IE11不支持ES6?

只要是完整的,這是我(簡化):

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

謝謝!

+2

根據[ES6兼容性表(https://kangax.github.io/compat-table/es6/),類不被IE11識別。 – Mikey

回答

4

恨重新打開這樣一個老問題,但它仍然高顯示出來的結果,所以我會添加我發現了什麼:

重申一下@Mikey和@REJH說,類不認可由IE11。

也就是說,像Babel這樣的工具將允許您將類轉換爲可在IE11上運行的東西。

+0

巴別爾出人意料地出色的真棒!謝謝一個男人。 –

3

@Mikey是對的。 IE11不承認這個語法類,因爲ES6規格: https://kangax.github.io/compat-table/es6/

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

我仍然不知道,如果以下是定義一個靜態類正確的方式,但它的工作原理:

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

只是把它放在這裏,所以這個問題有一些答案,可以幫助人們去。如果有以上選擇,我喜歡聽到這些!

0

靜態類示例

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; 
})(); 
相關問題