在 Firefox 中使用 ES6 类时,我在获取构造函数的名称时遇到问题。在 Chromium 中它工作正常,但 Firefox 似乎有某种错误?在 Firefox 中,我只返回一个空字符串。有人知道解决方法吗?
class MyClass {}
let a = new MyClass();
console.log(a.constructor.name);
最佳答案
我认为这是一个错误(根据下面的评论)。
似乎指定显式构造函数在 Firefox 中表现出正确的行为(即使是最新版本 48)。
class MyClassWithConstructor {
constructor() {
console.log("Explicit Constructor")
}
}
class MyClassWithoutConstructor {}
$('#with').click(function() {
let tmp = new MyClassWithConstructor();
alert("MyClassWithConstructor name = " + tmp.constructor.name);
})
$('#without').click(function() {
let tmp = new MyClassWithoutConstructor();
alert("MyClassWithConstructor name = " + tmp.constructor.name);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id=with>With Constructor</button>
<button id=without>Without Constructor</button>
这是 JSFiddle 的链接:https://jsfiddle.net/jc7g5crp/
关于javascript - Firefox ES6,获取类构造函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285126/