Inheriting from a Class: Default Constructors and Legacy Classes

Learn about the default constructors and legacy classes in inheritance.

Using default constructors

Recall that if you do not write a constructor, JavaScript provides a default constructor for the class. The same rule applies to derived classes, with one bonus: the default constructor automatically calls super to pass any arguments to the base class. This feature is nice and removes the need to write silly constructors that do not do anything except pass data to the base class. Once you get used to it, you may wish languages like Java and C# had this feature.

Default constructor in action

Let’s extend an AwesomePerson class from the Person class we wrote in the previous lesson. Our AwesomePerson class is not going to introduce any new fields; it will only override a property of the base class. If we were forced to write a constructor, it would be pure noise to merely forward the parameters to the base class. Thankfully, the default constructor acts as a nice quiet pass-through constructor, as you can see here:

Get hands-on with 1200+ tech skills courses.