


Comparative analysis of several inheritance methods in Javascript programming_javascript skills
This article analyzes the comparison of several inheritance methods in Javascript programming with examples. Share it with everyone for your reference, the details are as follows:
Opening
In the 'strict' sense, JavaScript is not a true object-oriented language. The reason for this statement is generally that JavaScript as a weakly typed language is very different from the inheritance method of strong languages such as Java or C#, so it is a non-mainstream object-oriented method by default, and there are even many The book describes it as a 'not fully object-oriented' language. In fact, I personally feel that the method is not important, what is important is whether it has object-oriented thinking. People who say that JavaScript is not an object-oriented language may not have studied the inheritance method of JavaScript in depth, so I wrote this article for communication.
Why you need to use javascript to implement inheritance
The performance of early PC machines is really not flattering. All the pressure was on the server side, and the client browser was purely a decoration. Coupled with the popular table layout and telephone line Internet access at that time, browsing a web page was very slow; now the Internet era is developing rapidly, personal computer hardware has been greatly improved, and the performance of client browsers is also very disappointing. The model of web development is also quietly changing: the server is no longer as "hard" as before. Instead, the browser should take on as many tasks as possible. In this way, the pressure is distributed to each client. Not only does the enterprise Saving costs also makes web front-end development more interesting - more and more front-end frameworks are emerging, and even many front-end MVC frameworks have emerged. In this context, the role of JavaScript is definitely not just to do some simple verification, send some requests or operate some DOM. It needs to play more roles like front-end routing and business layer, and JavaScript needs to do a lot of logical tasks. , which includes the abstraction of front-end data (i.e. model), and only by using object-oriented thinking can the abstracted data be processed well, so inheritance is very important here.
Start with a simple need
Now extract a model named Person from the front desk, which has basic attributes name and age. By default, everyone can speak, so the speaking function is placed on the prototype object for each instance to enjoy. Now for Man, it needs to inherit the basic attributes of Person and add its own unique attributes on this basis.
function Person (name, age) { this.name = name; this.age = age; } Person.prototype.say = function(){ console.log('hello, my name is ' + this.name); }; function Man() { //my own properties }
The following introduces several mainstream inheritance methods.
1. Prototype chain inheritance
function Person (name, age) { this.name = name; this.age = age; } Person.prototype.say = function(){ console.log('hello, my name is ' + this.name); }; function Man() { } Man.prototype = new Person('pursue'); var man1 = new Man(); man1.say(); //hello, my name is pursue var man2 = new Man(); console.log(man1.say === man2.say);//true console.log(man1.name === man2.name);//true
This inheritance method is very direct. In order to obtain all attribute methods of Person (instance and prototype), the instance new Person('pursue') of the parent class is directly assigned to the prototype of the subclass. In fact, the subclass The class instances man1 and man2 themselves are completely empty objects. All attributes and methods have to be found on the prototype chain, so the attributes and methods found are the same.
Therefore, it is unrealistic to directly use prototype chain inheritance.
2. Use constructor inheritance
function Person (name, age) { this.name = name; this.age = age; } Person.prototype.say = function(){ console.log('hello, my name is ' + this.name); }; function Man(name, age) { Person.apply(this, arguments); } //Man.prototype = new Person('pursue'); var man1 = new Man('joe'); var man2 = new Man('david'); console.log(man1.name === man2.name);//false man1.say(); //say is not a function
Here the subclass uses apply in the constructor to call the constructor of the parent class, so as to achieve the effect of inheriting the properties of the parent class. It is much better than directly using the prototype chain. At least each instance has its own share. resources, but this method can only inherit the instance attributes of the parent class, so the say method cannot be found. In order to inherit all the attributes and methods of the parent class, the prototype chain must be modified, thus introducing the combined inheritance method.
3. Combination inheritance
function Person (name, age) { this.name = name; this.age = age; } Person.prototype.say = function(){ console.log('hello, my name is ' + this.name); }; function Man(name, age) { Person.apply(this, arguments); } Man.prototype = new Person(); var man1 = new Man('joe'); var man2 = new Man('david'); console.log(man1.name === man2.name);//false console.log(man1.say === man2.say);//true man1.say(); //hello, my name is joe
It should be noted that the instance attributes of man1 and man2 actually override the prototype attributes, but they do not override the say method on the prototype (because they do not have it), so here man1.say === man2.say still returns true, so be careful not to override the prototype property as it is common to all instances.
4. Parasitic combination inheritance
To be honest, I really don’t know the name of the following form, but it is indeed the most popular and classic JavaScript inheritance method. In fact, you only need to understand the structure of the prototype object:
function Person (name, age) { this.name = name; this.age = age; } Person.prototype.say = function(){ console.log('hello, my name is ' + this.name); }; function Man(name, age) { Person.apply(this, arguments); } Man.prototype = Object.create(Person.prototype);//a. Man.prototype.constructor = Man;//b. var man1 = new Man('pursue'); var man2 = new Man('joe'); console.log(man1.say == man2.say); console.log(man1.name == man2.name);
In fact, the only difference between parasitic combination inheritance and the above combination inheritance is the way to construct the prototype object of the subclass (a. and b.). The Object.creat(obj) method is used here, which will process the incoming obj The object is shallow copied, similar to:
function create(obj){ function T(){}; T.prototype = obj; return new T(); }
Therefore, a. will connect the prototype object of the subclass with the prototype object of the parent class well, instead of directly copying the prototype of the subclass like the general combined inheritance (such as Man.prototype = new Person();), this is just a very violent overwriting of attributes. The parasitic combination inheritance method inherits instance attributes and prototype attributes separately, which is more reasonable in implementation.
Note: Code b. will not change the result of instanceof, but it is more rigorous for scenarios where construcor is needed.
I hope this article will be helpful to everyone in JavaScript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Nowadays, the performance and functions of mobile phones are becoming more and more powerful. Almost all mobile phones are equipped with convenient NFC functions to facilitate users for mobile payment and identity authentication. However, some Xiaomi 14Pro users may not know how to enable the NFC function. Next, let me introduce it to you in detail. How to enable nfc function on Xiaomi 14Pro? Step 1: Open the settings menu of your phone. Step 2: Find and click the "Connect and Share" or "Wireless & Networks" option. Step 3: In the Connection & Sharing or Wireless & Networks menu, find and click "NFC & Payments". Step 4: Find and click "NFC Switch". Normally, the default is off. Step 5: On the NFC switch page, click the switch button to switch it to on.

Sliding the screen through the air is a feature of Huawei that is highly praised in the Huawei mate60 series. This feature uses the laser sensor on the phone and the 3D depth camera of the front camera to complete a series of functions that do not require The function of touching the screen is, for example, to use TikTok from a distance. But how should Huawei Pocket 2 use TikTok from a distance? How to take screenshots from the air with Huawei Pocket2? 1. Open the settings of Huawei Pocket2 2. Then select [Accessibility]. 3. Click to open [Smart Perception]. 4. Just turn on the [Air Swipe Screen], [Air Screenshot], and [Air Press] switches. 5. When using it, you need to stand 20~40CM away from the screen, open your palm, and wait until the palm icon appears on the screen.

The CAD files of the iPhone 16 Pro have been exposed, and the design is consistent with previous rumors. Last fall, the iPhone 15 Pro added an Action button, and this fall, Apple appears to be planning to make minor adjustments to the size of the hardware. Adding a Capture button According to rumors, the iPhone 16 Pro may add a second new button, which will be the second consecutive year to add a new button after last year. It is rumored that the new Capture button will be set on the lower right side of the iPhone 16 Pro. This design is expected to make camera control more convenient and also allow the Action button to be used for other functions. This button will no longer be just an ordinary shutter button. Regarding the camera, from the current iP

WPS is our commonly used office software. When editing long articles, the fonts are often too small to be seen clearly, so the fonts and the entire document are adjusted. For example: adjusting the line spacing of the document will make the entire document very clear. I suggest that all friends learn this operation step. I will share it with you today. The specific operation steps are as follows, come and take a look! Open the WPS text file you want to adjust, find the paragraph setting toolbar in the [Start] menu, and you will see the small line spacing setting icon (shown as a red circle in the picture). 2. Click the small inverted triangle in the lower right corner of the line spacing setting, and the corresponding line spacing value will appear. You can choose 1 to 3 times the line spacing (as shown by the arrow in the figure). 3. Or right-click the paragraph and it will appear.

According to statistics on March 2, the total TVL of Bitcoin’s second-layer network MerlinChain has reached US$3 billion. Among them, Bitcoin ecological assets accounted for 90.83%, including BTC worth US$1.596 billion and BRC-20 assets worth US$404 million. Last month, MerlinChain’s total TVL reached US$1.97 billion within 14 days of launching staking activities, surpassing Blast, which was launched in November last year and is also the most recent and equally eye-catching. On February 26, the total value of NFTs in the MerlinChain ecosystem exceeded US$420 million, becoming the public chain project with the highest NFT market value besides Ethereum. Project Introduction MerlinChain is an OKX support

Comparison and analysis of advantages and disadvantages of PHP7.2 and 5. PHP is an extremely popular server-side scripting language and is widely used in Web development. However, PHP is constantly being updated and improved in different versions to meet changing needs. Currently, PHP7.2 is the latest version, which has many noteworthy differences and improvements compared with the previous PHP5 version. In this article, we will compare PHP7.2 and PHP5 versions, analyze their advantages and disadvantages, and provide specific code examples. 1. Performance PH

Differences and comparative analysis between C language and PHP C language and PHP are both common programming languages, but they have obvious differences in many aspects. This article will conduct a comparative analysis of C language and PHP and illustrate the differences between them through specific code examples. 1. Syntax and usage: C language: C language is a process-oriented programming language, mainly used for system-level programming and embedded development. The syntax of C language is relatively simple and low-level, can directly operate memory, and is efficient and flexible. C language emphasizes the programmer's completeness of the program

The progress of the times has made many people's incomes higher and higher, and the mobile phones they usually use will be changed frequently. The Xiaomi Mi 14 Ultra recently launched by Xiaomi must be familiar to users. It has very high performance configuration and can provide users with more In order to provide a comfortable and smooth experience, new mobile phones will inevitably encounter many functions that are not used. For example, how to use Xiaomi 14UltraAI smart image expansion? Come and take a look at the usage tutorial below! How to use Xiaomi 14UltraAI smart image expansion? First open Xiaomi 14Ultra, enter the photo album, select the picture you want to enlarge, and enter the photo album editing option. Click Crop Rotate, click Crop, and click Smart Expand in the selection that appears. Finally, choose the way to expand the image according to your own needs.
