Inheritance in Javascript (ES6)

INHERITANCE :

The extends keyword is used in class declarations or class expressions to create a class which is a child of another class.


class ChildClass extends ParentClass { ... }


The extends keyword can be used to subclass custom classes as well as built-in objects. The .prototype of the extension must be an Object or null.


Examples : - 

Using extends - 


Please click on this link  https://googlechrome.github.io/samples/classes-es6/index.html for a better example.

Using extends with built-in objects - 


class myDate extends Date {
  constructor() {
    super();
  }

  getFormattedDate() {
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    return this.getDate() + '-' + months[this.getMonth()] + '-' + this.getFullYear();
  }
}


Extending null

Extending from null works like with a normal class, except that the prototype object does not inherit from Object.prototype.
class nullExtends extends null {
  constructor() {}
}

Object.getPrototypeOf(nullExtends); // Function.prototype
Object.getPrototypeOf(nullExtends.prototype) // null

Comments

  1. Hard Rock Hotel and Casino Las Vegas Map & Directions
    Harrah's Las Vegas Map & Directions · Harrah's Las 부산광역 출장안마 Vegas 삼척 출장안마 Casino & Hotel 거제 출장샵 is a hotel and casino 아산 출장안마 located in Las 부천 출장마사지 Vegas, Nevada, United States. · Map

    ReplyDelete

Post a Comment

Popular posts from this blog

What is MQTT protocol & its use

What the hack is Call, bind & apply

What is Provider in Angular Js?