Inheriting from a Class: Default Constructors and Legacy Classes
Explore how JavaScript supports inheritance through default constructors that automatically pass arguments to base classes, reducing boilerplate code. Learn to extend classes created with both modern class syntax and legacy function syntax for backward compatibility. This lesson helps you write cleaner subclass constructors and manage inheritance effectively in JavaScript.
We'll cover the following...
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 ...