JavaScript has evolved significantly since its early days, introducing numerous features aimed at improving developer experience and enhancing productivity. Among those advancements lies the introduction of ES6 classes and the super
keyword – game changers when working with inheritance hierarchies.
Let's dive into the world of super
keywords in JavaScript classes and discover practical ways they can help simplify your everyday programming tasks.
What is the ‘super’ Keyword?
In JavaScript, the super
keyword serves dual purposes: invoking parent constructors during instantiation and referring to parent class methods or properties within child classes. By leveraging super
, developers ensure seamless interaction between parent and child classes while promoting encapsulation and reusability.
Invoking Parent Constructors
When creating a child class, we often initialize shared state through inheritance from a parent class. The super()
function takes care of executing the parent constructor first, ensuring proper initialization order. Here's an example…