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);
A constructor is a function that is called each time an object is created (also referred to as instantiated). The User constructor creates the properties of the object (this.name, this. age, this. email) and assigns them the value of the parameters passed to it (name, age, email).
The super keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor. The super. prop and super[expr] expressions are valid in any method definition in both classes and object literals. The super(... args) expression is valid in class constructors.
Subclassing is a term that refers to inheriting properties for a new object from a base or superclass object. In traditional object-oriented programming, a class B is able to extend another class A . Here we consider A a superclass and B a subclass of A . As such, all instances of B inherit the methods from A .
What Does Constructor Mean? A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With