3 ways to implement inheritance in JavaScript _ basics

Source: Internet
Author: User
Tags inheritance require

Implementing class inheritance with Object.create

Here is an example of the official website

Shape-superclass
function Shape () {
 this.x = 0;
 This.y = 0;
}

Shape.prototype.move = function (x, y) {
  this.x = x;
  This.y = y;
  Console.info ("Shape moved.");

Rectangle-subclass
function Rectangle () {
 shape.call (this);//call super constructor.
}

Rectangle.prototype = Object.create (shape.prototype);

var rect = new Rectangle ();

Rect instanceof Rectangle//true.
Rect instanceof Shape//true.

Rect.move (1, 1); Outputs, "Shape moved."

At this point the constructor of the rectangle prototype points to the parent class, and if you need to use its own constructs, you can specify it manually, as follows

Rectangle.prototype.constructor = Rectangle;

Use the Utilities toolkit to bring your own util.inherites

Grammar

Util.inherits (constructor, Superconstructor)
Example

Const UTIL = require (' util ');
Const Eventemitter = require (' events ');

function MyStream () {
  eventemitter.call (this);
}

Util.inherits (MyStream, eventemitter);

MyStream.prototype.write = function (data) {
  this.emit (' data ', data);
}

var stream = new MyStream ();

Console.log (stream instanceof Eventemitter); True
console.log (mystream.super_ = = = Eventemitter);//True

stream.on (' Data ', (data) => {
 Console.log (' Received data: "${data}");
}
Stream.Write (' It works! '); Received data: "It works!"

Also very simple example, in fact, the source code with the new features of ES6, we look at

Exports.inherits = function (ctor, superctor) {

 if (ctor = = Undefined | | ctor = = NULL)
  throw new TypeError (' The C Onstructor to "inherits" must is not is ' +
            ' null or undefined ');

 if (Superctor = = Undefined | | superctor = = NULL)
  throw new TypeError (' The Super constructor to "inherits" must " +
            ' be null or undefined ');

 if (Superctor.prototype = = undefined)
  throw new TypeError (' The Super constructor to "inherits" must ' +
            ' have a P Rototype ');

 Ctor.super_ = Superctor;
 Object.setprototypeof (Ctor.prototype, Superctor.prototype);
};

Where Object.setprototypeof is the ES6 new attribute that sets the prototype of a specified object to another object or null

Grammar

Object.setprototypeof (obj, prototype)
obj is an object that will be set to prototype
Prototype is the new prototype for obj (can be an object or null).

If set to NULL, the following example

Object.setprototypeof ({}, NULL);
Feel setprototypeof is really a person such as its name, specially engaged in prototype to play.
So how does this thing work? At this time need to use the Guru __proto__

object.setprototypeof = object.setprototypeof | | function (obj, proto) {
 obj.__proto__ = proto;
 return obj; 
}

It is good to give Proto to Obj.__proto__.

Using the Extends keyword

Familiar with Java students should be very familiar with this keyword, the Java inheritance is implemented by it.
ES6 's new class keyword is syntactic sugar, which is essentially a function.

In the following example, a class named Polygon is defined, and then a class Square that inherits from Polygon is defined. Note that the super () used in the constructor, supper () can only be used in the constructor, and the super function must be invoked before this can be used.

Class Polygon {
 constructor (height, width) {
  this.name = ' Polygon ';
  this.height = height;
  This.width = width;
 }
}

Class Square extends Polygon {
 Constructor (length) {
  super (length);
  this.name = ' Square ';
 }


Use the keyword after the various settings of the prototype, the keyword has been encapsulated, very fast and convenient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.