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
question How to modify CSS pseudo-class style wit...
The multi-site feature of WordPress allows you to...
Preface When the system space usage is too large ...
What is HTTP? When we want to browse a website, w...
This article example shares the specific code of ...
Table of contents uni-app Introduction HTML part ...
The party that creates a new connection is equiva...
The function to be implemented today is the follo...
This article uses examples to illustrate how to v...
Application software generally has such business ...
This article example shares the specific code of ...
Question: How to achieve a rounded rectangle usin...
In the table header, you can define the light bor...
1.What is margin? Margin is used to control the sp...
I will not introduce what CUDA is, but will direc...