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
Table of contents 1. Basic Examples 2. Computed p...
Nginx log description Through access logs, you ca...
Table of contents Preface Step 1: Setup and front...
Table of contents Scenario Task idea analyze Conc...
Table of contents background Effect Ideas backgro...
Background - Online Alert An online server issued...
Things to note 1. First, you need to create a my....
After getting used to VM, switching to BOX is a l...
Table of contents Preface 1. The database name or...
This article shares with you how to use bootstrap...
Dockerfile is a file used to build a docker image...
As shown below: The test command determines wheth...
1. Preparation 1.1 harbor download harbor downloa...
When using lepus3.7 to monitor the MySQL database...
Here's a solution to the problem where margin...