Firefox ES6,获取类构造函数名称

6

我在使用ES6类时在Firefox中无法获取构造函数的名称。在Chromium中没有问题,但是Firefox似乎有某种bug?在Firefox中只返回一个空字符串。有人知道解决方法吗?

class MyClass {}
let a = new MyClass();
console.log(a.constructor.name);

1
所以,当您首次访问MyClass.name时,它起作用。当您首先访问.constructor.name时,它是一个空字符串。很奇怪,我猜这是个bug。这里有一个演示的Fiddle:https://jsfiddle.net/gveopgu8/ - Alexander O'Mara
2
这些问题可能有一定关联:https://bugzilla.mozilla.org/show_bug.cgi?id=1192412 https://bugzilla.mozilla.org/show_bug.cgi?id=1280042 - Alexander O'Mara
2
也许有个好消息,我在Firefox Developer Edition(基于50.0)中无法重现此问题,因此它可能已经在即将发布的版本中得到修复。 - Alexander O'Mara
1个回答

1
根据下方的评论,我认为这是一个错误。在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/


2
这是一个ES6的特性,在FF中似乎是一个bug。 - Bergi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接