Preface: In our previous article, we introduced JavaScript prototypes. Why not introduce the prototype chain together? Because the prototype chain in JavaScript is a difficult point and a must-ask question in an interview, let’s learn it now. 1. Prototype Relationship Every function in It is worth noting that the Now let's test the relationship between the Object constructor and the prototype. The sample code is as follows: // First, Object is a constructor function, so it has a prototype property var result = Object.prototype console.log(result) // Get a prototype object/* * The constructor property of the prototype object -> returns the constructor associated with it * Object.getPrototypeOf(result) returns the prototype pointing to the constructor */ var result2 = result.constructor console.log(result2) // [Function: Object] var result3 = Object.getPrototypeOf(result) console.log(result3) // null The diagram is shown below: When we get the prototype of 2. Prototype chainTo make it easier for us to understand what prototype chaining is, first take a look at the following code: function Person(name) { this.name = name } var PP = Person.prototype var PPC = PP.constructor // Verify that it is the same as the constructor console.log(PPC === Person) // true // Instantiate Person var person = new Person('Yiwan Zhou') // Get the prototype of the instantiated Person object var pP = Object.getPrototypeOf(person) // Verify that the prototype of the instantiated Person object points to the prototype of the constructor console.log(pP === PP) // true In fact, all constructors are inherited from Object by default, as tested in the following code: // Get the prototype of Person.prototype var PPP = Object.getPrototypeOf(PP) var OP = Object.prototype // Check if the two are equal console.log(PPP === OP) // true The code above is not very clear, so I drew a picture to help you understand it: The red line in the above picture is the prototype chain. The prototype chain points to the relationship in the prototype until the final result is null, which is We can use 3. ConclusionTwo pictures are used to explain the relationship between prototypes in JavaScript and what the prototype chain is. Finally, the end point of the prototype chain is introduced. This concludes this article about understanding the prototype chain in JavaScript with two pictures. For more information about the JavaScript prototype chain, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Common DIV tasks (Part 2) — Transform into editors and various DIY applications of DIV
>>: Several ways to implement CSS height changing with width ratio
What is the reason for the Last_IO_Errno:1236 err...
The correspondence between tensorflow version and...
Sometimes the theme of a project cannot satisfy e...
1. Two ways to specify the character set of the h...
Benefits of a programmatic approach 1. Global con...
The key is that the local server does not have wr...
Talk about the scene Send Email Embedding HTML in...
Table of contents What is MVCC Mysql lock and tra...
When receiving this requirement, Baidu found many...
Table of contents Preface Rendering setTable comp...
Solution to Host 'xxxx' is not allowed to...
Install using the MSI installation package Downlo...
Select the category selection. After testing, IE ...
The so-called favicon, which is the abbreviation o...
Preface In JavaScript, this is the function calli...