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
Previously, I summarized how to use CSS to achieve...
Preview versions of Safari (Technology Preview 10...
This article shares the specific code of JavaScri...
When you get a new Linux server, you generally ha...
Achieve resultsImplementation Code html <base ...
Written in front No matter how well the code is w...
Background Recently, I encountered such a problem...
Chinese Tutorial https://www.ncnynl.com/category/...
Preface The Boost library is a portable, source-c...
1. Configure Docker remote connection port Locate...
When building a website, HTML language may seem un...
Preface I have been working on some front-end pro...
CSS style: Copy code The code is as follows: <s...
This article example shares the specific code of ...
MySQL5.7.21 installation and password setting tut...