使用eval方法从Firefox中的字符串中获取类

pre*_*per 2 javascript firefox eval es6-class

我尝试过的(在chrome中有效)

var class_str = "class Test {};";
var a = eval(class_str);
console.log(new a());
Run Code Online (Sandbox Code Playgroud)

在Firefox 46中引发以下错误:

TypeError: a is not a constructor
Run Code Online (Sandbox Code Playgroud)

a未定义并使用new A()返回ReferenceError: A is not defined.

Firefox有什么不同?

pre*_*per 6

将整个类字符串放在括号中.

固定代码:

var class_str = "(class Test {})";
var a = eval(class_str);
console.log(new a());
Run Code Online (Sandbox Code Playgroud)