6

I have problems getting the name of the constructor when using ES6 classes in Firefox. In Chromium it works fine, but Firefox seem to have some kind of bug? In Firefox I only get an empty string back. Anyone that knows of a workaround?

class MyClass {}
let a = new MyClass();
console.log(a.constructor.name);
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
tirithen
  • 3,219
  • 11
  • 41
  • 65
  • 1
    So, when you access `MyClass.name` first, it works. When you access `.constructor.name` first, it's a blank string. Weird, I'm guessing bug. Here's a Fiddle to demonstrate: https://jsfiddle.net/gveopgu8/ – Alexander O'Mara Sep 02 '16 at 06:23
  • 2
    These issues may have some relation: https://bugzilla.mozilla.org/show_bug.cgi?id=1192412 https://bugzilla.mozilla.org/show_bug.cgi?id=1280042 – Alexander O'Mara Sep 02 '16 at 06:30
  • 2
    Maybe some good news, I cannot reproduce in Firefox Developer Edition (based on 50.0), so it may have already been fixed in an upcoming version. – Alexander O'Mara Sep 02 '16 at 06:34

1 Answers1

1

I think it is a bug (according to the comment below).

It appears that specifying an explicit constructor exhibits the correct behavior in Firefox (even the latest version 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>

Here's a link to JSFiddle: https://jsfiddle.net/jc7g5crp/

Nick
  • 4,002
  • 4
  • 30
  • 41