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
This article shares the specific steps of install...
Preface I'm currently working on the data ana...
Preface NAT forwarding: Simply put, NAT is the us...
On mobile devices, flex layout is very useful. It...
Table of contents Preface How to switch between m...
When using Docker in a production environment, da...
To beautify the table, you can set different bord...
Table of contents 1. MySQL trigger creation: 1. M...
nginx is our most commonly used server, often use...
Original source: www.bamagazine.com There are nar...
After reading the following article, you can depl...
Install vsftpd $ sudo apt-get install vsftpd -y S...
Classification of website experience 1. Sensory e...
Problem Description MySQL reports an error when s...
1. MS SQL SERVER 2005 --1. Clear the log exec(...