Prototype chain diagramEssential knowledge for prototyping To understand the prototype, you must understand three properties: 1.__proto__ and constructor attributes are unique to objects; 2. The prototype property is unique to functions; 3. In js, functions are also a type of object, so functions also have properties __proto__ and constructor; Five rules of prototype: 1. All reference types (objects, arrays, functions) have object characteristics, that is, they can freely extend properties 2. All reference types (objects, arrays, functions) have a __proto__ (implicit prototype) attribute, which is a normal object 3. All functions have a prototype (explicit prototype) property, which is also a normal object 4. All reference types (objects, arrays, functions) __proto__ values point to the prototype of its constructor 5. When trying to get the property of an object, if the variable itself does not have this property, it will look for it in its __proto__ prototype property (display prototype)First create a constructor var Parent = function(){ } //Define a function, it is just an ordinary function var p1 = new Parent(); //Through the keyword new, Parent becomes a constructor //Creates an instance of a Parent constructor p1
Parent is the constructor, Parent.prototype is the prototype The properties and methods added to proto property (implicit prototype)The __proto__ property is unique to objects (including functions). Every object has a __proto__ property, which points to the prototype object of the object. p1.__proto__ === Parent.prototype; // true __proto__ is usually called the implicit prototype, and prototype is usually called the explicit prototype. It can be said that the implicit prototype of an object points to the explicit prototype of the constructor of the object. Then the property methods defined on the explicit prototype are passed to the instance of the constructor through the implicit prototype. This way, the instance can easily access the methods and properties on the constructor prototype. The implicit prototype of Parent.prototype.__proto__ === Object.prototype; //true This introduces the concept of prototype chain. When Of course, if it is not found on Object.prototype, it will look for it in The constructor propertySince the constructor function accesses the prototype through prototype, the prototype should also be able to access the constructor function through some means, which is the constructor. As in the previous example, p1 is an object, and the constructor of p1 is Parent(). Parent's constructor is Function() p1.constructor => f Parent{} Parent.constructor => f Function() { [native code] } Function.constructor => ƒ Function() { [native code] } Function is the root constructor of all functions. From the example, we can see that SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Pure CSS to achieve left and right drag to change the layout size
>>: Examples of using html unordered list tags and ordered list tags
To achieve the background color flashing effect, j...
In this experiment, we configure MySQL standard a...
Table of contents 1. Prototype mode Example 1 Exa...
The current requirement is: there is a file uploa...
Development environment windows Development Tools...
Preface: Docker is an open source application con...
【Problem Analysis】 We can use the chown command. ...
We usually use the <ul><li> tags, but ...
Error message: Job for mysqld.service failed beca...
Simply pull the image, create a container and run...
There are many tags and elements in the HTML head ...
Many friends found that the box model is a square...
This article shares the specific code for impleme...
1. Error reproduction I can access the MySQL data...
1. Apache 2.4.41 installation and configuration T...