Inheritance
Inheritance
JavaScript's inheritance works differently from inheritance in other languages, which can be very confusing. ES6 classes provide a syntactic sugar attempting to alleviate the issues with using prototypical inheritance present in ES5.
To illustrate this, let's image we have a zoo application where types of birds are created. In classical inheritance, we define a base class and then subclass it to create a derived class.
Subclassing
The example code below shows how to derive Penguin
from Bird
using the extends keyword. Also pay attention to the super keyword used in the subclass constructor of Penguin
, it is used to pass the argument to the base class Bird
's constructor.
The Bird
class defines the method walk which is inherited by the Penguin
class and is available for use by instance of Penguin
objects. Likewise the Penguin
class defines the method swim which is not avilable to Bird
objects. Inheritance works top-down from base class to its subclass.
Object Initialization
The class constructor is called when an object is created using the new operator, it will be called before the object is fully created. A consturctor is used to pass in arguments to initialize the newly created object.
The order of object creation starts from its base class and then moves down to any subclass(es).
Below we show how prototypal inheritance was done before class was introduced to JavaScript.
Last updated