Inheritance - The ES6 way
inheritance in ES6, accessing constructors and other class methods at various places in the code, introduction to concise method syntax and Javascript code conventions
Let’s see the ES6 version of inheritance. As we’ll see later, the ES5 and ES6 versions are not equivalent, we just describe the same problem domain with ES6 code.
Node.js
class Shape {constructor( color ) {this.color = color;}getColor() {return this.color;}}class Rectangle extends Shape {constructor( color, width, height ) {super( color );this.width = width;this.height = height;}getArea() {return this.width * this.height;}}let rectangle = new Rectangle( 'red', 5, 8 );console.log( "Area:\t\t" + rectangle.getArea() );console.log( "Color:\t\t" + rectangle.getColor() );console.log( "toString:\t" + rectangle.toString() );
Classes may encapsulate
- a constructor function
- additional operations extending the prototype
- reference to the parent prototype.
Notice the following:
-
The ...