The relationship between the constructor instance and prototype1. Any function has a prototype property, which is an object function F () {} console.log(F.prototype) // => object //Prototype object F.prototype.sayHi = function () { console.log('hi!') } 2. The prototype object of the constructor has a constructor property by default, which points to the function where the prototype object is located. console.log(F.constructor === F) // => true //Indicates this 3. The instance object obtained through the constructor will contain a pointer to the prototype object of the constructor _proto_ var instance = new F() console.log(instance.__proto__ === F.prototype) // => true This means that the instance object created by the current constructor contains a pointer inside, which is Therefore, we can directly access the members on the prototype object using the instance example: instance.sayHi() // => print hi! Notice Prototype Property
This means that we can define the properties and methods that all object instances need to share directly on the prototype object. example: function Person (name, age) { this.name = name this.age = age } console.log(Person.prototype) //Print prototype Person.prototype.type = 'human' //Mount human to the properties of the prototype object Person.prototype.sayName = function () { //You can also define functions console.log(this.name) } let p1 = new Person(...) let p2 = new Person(...) console.log(p1.sayName === p2.sayName) // => true We can see that the result printed by This is because Search principles for attributes or membersWe know that multiple instance objects can share properties or members in the prototype object, so how is this sharing mechanism implemented in JS? This has to mention the search principle of attributes Whenever the code reads a property of an instance object, a search is performed for a property or member with the given name. The search process is as follows: 1. Start the search from the object instance itself 2. If an attribute with a given name is found in the instance object, the value of the attribute is returned 3. If not found, continue searching for the prototype object pointed to by the pointer contained in the instance object (mentioned above), and look for the attribute with the given name in the prototype object 4. If this property is found in the prototype object, the value of the property is returned When executing SummarizeThe above is the basic principle of multiple instance objects sharing the properties and methods mounted by the prototype! This 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:
|
<<: MySQL graphical management tool Navicat installation steps
>>: HTML uses canvas to implement bullet screen function
Use JS to implement object-oriented methods to ac...
0. Prepare relevant tables for the following test...
1. Official website address The official website ...
Table of contents 1. Concept 2. Environmental Des...
1. Download: http://www.oracle.com/technetwork/ja...
Preface This article will share some docker-compo...
Windows cmd telnet format: telnet ip port case: t...
What is bond NIC bond is a technology that is com...
Table of contents Preface 1. Get the length of a ...
Solution function mergeImgs(list) { const imgDom ...
Achieve results Code html <div class="css...
Several parts of Compose deal with environment va...
First, perform a simple Docker installation. To c...
This article shares the specific code of the jQue...
1. Command Introduction nl (Number of Lines) adds...