ES6 inheritance
The class
construct introduced by the ECMAScript 6 specifications also brings a new way to define inheritance. Using a syntax similar to most common classical Object-Oriented Programming languages, we can make a class inherit from another one. Let's take the previous example of person and developer and rewrite it using classes:
class Person { constructor(name, surname) { this.name = name; this.surname = surname; } } class Developer extends Person { constructor(name, surname, knownLanguage) { super(name, surname); this. knownLanguage = knownLanguage; } }
In this example, we have highlighted the relevant parts of inheritance. As we can see, the class Developer
inherits from the class Person
using the extends
keyword. Moreover, in its constructor the Developer
class calls its parent class using the super
keyword. This code is a more compact and readable way to get the same functionalities using the following constructor functions...