The mainstream ways of implementing inheritance in non-ES6 code can be divided into: 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) shortcoming (1) You can only inherit the properties and methods of the parent class instance, not the properties and methods on the prototype 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 shortcoming 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 shortcoming Parasitic Compositional Inheritanceaccomplish 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? advantage 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:
|
<<: Detailed explanation of Nginx regular expressions
>>: Solution to forgetting MySQL root password in MACOS
1. Error details Once when manually performing a ...
How to install MySQL 5.7 in Ubuntu 16.04? Install...
1. Single machine environment construction# 1.1 D...
In Vue, we generally have front-end and back-end ...
Index definition: It is a separate database struc...
Table of contents 1. Introduction to Jenkins 2. I...
Table of contents Introduction Step 1 Step 2: Cre...
Table of contents 1. Query Optimization 1. MySQL ...
A colleague asked me what N and M mean in the MyS...
1. The role of brackets 1.1 Square brackets [ ] W...
Table of contents 1. Introduction 2. Use 1. Diffe...
View Docker Network docker network ls [root@maste...
The implementation principle of Vue2.0/3.0 two-wa...
According to the principles of W3C, each start tag...
Step 1: Create a Django project Open the terminal...