我在使用ES6类时在Firefox中无法获取构造函数的名称。在Chromium中没有问题,但是Firefox似乎有某种bug?在Firefox中只返回一个空字符串。有人知道解决方法吗?
class MyClass {}
let a = new MyClass();
console.log(a.constructor.name);
我在使用ES6类时在Firefox中无法获取构造函数的名称。在Chromium中没有问题,但是Firefox似乎有某种bug?在Firefox中只返回一个空字符串。有人知道解决方法吗?
class MyClass {}
let a = new MyClass();
console.log(a.constructor.name);
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/
MyClass.name
时,它起作用。当您首先访问.constructor.name
时,它是一个空字符串。很奇怪,我猜这是个bug。这里有一个演示的Fiddle:https://jsfiddle.net/gveopgu8/ - Alexander O'Mara