1. IntroductionBefore learning inheritance, you need to have a certain understanding of the prototype chain. If you don’t understand, you can read another article of mine first, which has a more detailed explanation of the prototype chain: Detailed Explanation of JavaScript Prototype Chain. If you already understand, please continue. I wrote a blog post before that listed all the inheritance methods, but I found it too long to read it all at once, and it is not conducive to absorbing knowledge, so I will separate the composite inheritance part first, and then make up the parasitic part later. 2. Prototype chain inheritance The parent class instance is used as the prototype of the child class. The implicit prototype Read the following picture to understand the code clearly: //Father class function father() { this.fatherAttr = ["fatherAttr"]; } //Properties on the prototype of the parent class father.prototype.checkProto = "checkProto"; //Subclass function child() {} // Use the father instance as the prototype of the child constructor child.prototype = new father(); child.prototype.constructor = child; //Two subclass instances const test1 = new child(); const test2 = new child(); console.log("Test 1:"); console.log("test1:", test1); console.log("test2:", test2); console.log("test1.fatherAttr:", test1.fatherAttr); console.log("test2.fatherAttr:", test2.fatherAttr); console.log("Test 2:"); test1.fatherAttr.push("newAttr"); console.log("test1.fatherAttr:", test1.fatherAttr); console.log("test2.fatherAttr:", test2.fatherAttr); console.log("Test 3:"); console.log("test1.checkProto:", test1.checkProto); Features:
3. Constructor inheritance Bind //Father class function father(params) { this.fatherAttr = ["fatherAttr"]; this.params = params; } //Properties on the prototype of the parent class father.prototype.checkProto = "checkProto"; //Subclass function child(params) { father.call(this, params); } //Two subclass instances const test1 = new child("params1"); const test2 = new child("params2"); console.log("Test 1:"); console.log("test1:", test1); console.log("test2:", test2); console.log("test1.fatherAttr:", test1.fatherAttr); console.log("test2.fatherAttr:", test2.fatherAttr); console.log("Test 2:"); test1.fatherAttr.push("newAttr"); console.log("test1.fatherAttr:", test1.fatherAttr); console.log("test2.fatherAttr:", test2.fatherAttr); console.log("Test 3:"); console.log("test1.checkProto:", test1.checkProto); Features:
4. Combination inheritanceCombining prototype chain inheritance and constructor inheritance, you can use them according to the characteristics of the two inheritances. //Father class function father(params) { this.fatherAttr = ["fatherAttr"]; this.params = params; } //Properties on the prototype of the parent class father.prototype.checkProto = "checkProto"; //Subclass function child(params) { //The second call to the parent class constructor father.call(this, params); } // Use the father instance as the prototype of the child constructor child.prototype = new father(); //The parent class constructor is called for the first time child.prototype.constructor = child; //Two instances const test1 = new child("params1"); //Jump from here to the subclass constructor and call the parent class constructor for the second time const test2 = new child("params2"); console.log("Test 1:"); console.log("test1:", test1); console.log("test2:", test2); console.log("test1.fatherAttr:", test1.fatherAttr); console.log("test2.fatherAttr:", test2.fatherAttr); console.log("Test 2:"); test1.fatherAttr.push("newAttr"); console.log("test1.fatherAttr:", test1.fatherAttr); console.log("test2.fatherAttr:", test2.fatherAttr); console.log("Test 3:"); console.log("test1.checkProto:", test1.checkProto); console.log("Test 4:"); delete test1.fatherAttr console.log("test1:", test1); console.log("test1.fatherAttr:", test1.fatherAttr); Features:
This is the end of this article on the detailed explanation of JavaScript combination and inheritance. For more relevant JavaScript combination and inheritance content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to build sonarqube using docker
>>: Some findings and thoughts about iframe
When setting display:flex, justify-content: space...
This article uses examples to illustrate the simp...
1. Download the mysql tar file: https://dev.mysql...
This effect is most common on our browser page. L...
As the title says, otherwise when the page is revi...
Table of contents Purpose npm init and package.js...
First find out where the configuration file is wh...
The default_server directive of nginx can define ...
Use vite to build a vue3 project You can quickly ...
Table of contents Common version introduction Com...
Detailed explanation of the misplacement of the in...
Table of contents What is a mind map? How to draw...
The excellence of Linux lies in its multi-user, m...
1. The ul tag has a padding value by default in M...
Table of contents 1. Demand Method 1 Method 2 Met...