Several ways to implement inheritance in JavaScript

Several ways to implement inheritance in JavaScript

The mainstream ways of implementing inheritance in non-ES6 code can be divided into:
Structural inheritance, prototype chain inheritance, structural inheritance + prototype chain inheritance combined inheritance, and inheritance methods derived from combined inheritance.

Structural inheritance (implemented with call)

accomplish

function Super(age){
 this.age = age;
 this.say = function(){
 console.log(this.age)
 }
}
function Child(name,age){
 Super.call(this,age)
 this.name = name;
}
var child = new Child("min",23)
console.log(child instanceof Super); // false
console.log(child instanceof Child); // true

advantage

(1) Multiple inheritance can be achieved (call multiple parent class objects)
(2) Parameters can be passed to the parent in the constructor

shortcoming

(1) You can only inherit the properties and methods of the parent class instance, not the properties and methods on the prototype
(2) The instance is not an instance of the parent class, but an instance of the child class

Prototype chain inheritance (implemented with the help of prototype chain)

accomplish

function Super(){
 this.getName = function(){
 console.log(this.name)
 }
}
function Child(name){
	this.name = name;
}
Child.prototype = new Super(); // You can pass the construction parameter here Child.prototype.constructor = Child;
var child = new Child("min");
console.log(child instanceof Super); // true
console.log(child instanceof Child); // true
console.log(child.constructor); // Child

advantage
(1) The parent class prototype properties and methods are accessible to child classes
(2) The instance is an instance of the subclass and also an instance of the parent class

shortcoming
(1) Multiple inheritance cannot be achieved (2) When creating a subclass instance, it is impossible to pass parameters to the parent class constructor

Combined inheritance (construction inheritance + prototype chain inheritance)

accomplish

function Super(age){
 this.age = age;
 this.getAge = function(){
 console.log(this.age);
 }
}
function Child(name,age){
 Super.call(this,age)
 this.name = name;
}
Child.prototype = new Super(1); 
Child.prototype.constructor = Child;
var child = new Child("min",23);
console.log(child instanceof Super); // true
console.log(child instanceof Child); // true
console.log(child.constructor); // Child

advantage
(1) Combining the advantages of construction + prototype chain inheritance

shortcoming
(1) Child.prototype = new Super(); is called once more, which results in some unnecessary properties in the prototype object, such as the age property in the example above.

Parasitic Compositional Inheritance

accomplish

function Super(age){
 this.age = age;
 this.getAge = function(){
 console.log(this.age)
 }
}
function Child(name,age){
 Super.call(this,age)
 this.name = name;
}
(function(){
 function Copy(){}
 Copy.prototype = Super.prototype;
 Child.prototype = new Copy();
})()
Child.prototype.constructor = Child;
var child = new Child("min",23);

Note: Why not use Child.prototype = Super.prototype directly?
Answer: Child.prototype.constructor = Child; The key code, writing Super.prototype above will also change (reference type, pointing to the same address)

advantage
(1) This should be the most perfect solution to implement inheritance. The es6 extends keyword also implements inheritance in this way after the code is converted by babel.

Extra: With (Object.create)

accomplish

function Super(age){
 this.age = age;
 this.getAge = function(){
 console.log(this.age)
 }
}
function Child(name,age){
 Super.call(this,age)
 this.name = name;
}
Child.prototype = Object.create(Super.prototype,{
 constructor:{ // Constructor repairs value: Child
 }
})
var child = new Child("min",23);
console.log(child instanceof Super); // true
console.log(child instanceof Child); // true
console.log(child.constructor); // Child

The above are the details of several ways to implement inheritance in JavaScript. For more information about JavaScript implementation inheritance, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Understanding JavaScript inheritance in one article
  • Summary of 6 common inheritance methods in js
  • 6 inheritance methods of JS advanced ES6
  • Six inheritance methods in JS and their advantages and disadvantages
  • Detailed explanation of native Javascript inheritance methods and their advantages and disadvantages
  • Detailed explanation of 6 ways of js inheritance
  • 6 JavaScript inheritance methods and their advantages and disadvantages (summary)
  • Examples of several common ways to implement inheritance in JS
  • Share several inheritance methods in JavaScript

<<:  Detailed explanation of Nginx regular expressions

>>:  Solution to forgetting MySQL root password in MACOS

Recommend

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

A brief analysis of CSS :is() and :where() coming to browsers soon

Preview versions of Safari (Technology Preview 10...

JavaScript to achieve tab switching effect

This article shares the specific code of JavaScri...

How to configure common software on Linux

When you get a new Linux server, you generally ha...

Image scrolling effect made with CSS3

Achieve resultsImplementation Code html <base ...

Detailed explanation of how Angular handles unexpected exception errors

Written in front No matter how well the code is w...

Nginx one domain name to access multiple projects method example

Background Recently, I encountered such a problem...

Basic tutorial on controlling Turtlebot3 mobile robot with ROS

Chinese Tutorial https://www.ncnynl.com/category/...

Complete steps to install boost library under linux

Preface The Boost library is a portable, source-c...

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

CSS style to center the HTML tag in the browser

CSS style: Copy code The code is as follows: <s...

Vue+Element realizes paging effect

This article example shares the specific code of ...

MySQL 5.7.21 installation and password configuration tutorial

MySQL5.7.21 installation and password setting tut...