使用eval方法从Firefox中的字符串中获取类 [英] Using eval method to get class from string in Firefox

查看:144
本文介绍了使用eval方法从Firefox中的字符串中获取类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  var class_str =class Test {};; 
var a = eval(class_str);
console.log(new a());

引发Firefox 46中的以下错误:

 TypeError:a不是构造函数

a 未定义,并使用 new A()返回 ReferenceError:A未定义



Firefox上有什么不同?

解决方案



修正码:

  var class_str =(class Test {}); 
var a = eval(class_str);
console.log(new a());


What I tried (which works in chrome)

var class_str = "class Test {};";
var a = eval(class_str);
console.log(new a());

Raises following error in Firefox 46:

TypeError: a is not a constructor

a is undefined and using new A() returns ReferenceError: A is not defined.

What is different on Firefox?

解决方案

Putting the whole class string in parentheses works.

Fixed code:

var class_str = "(class Test {})";
var a = eval(class_str);
console.log(new a());

这篇关于使用eval方法从Firefox中的字符串中获取类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆