Home > Web Front-end > JS Tutorial > body text

Detailed introduction to js prototype

小云云
Release: 2018-03-06 13:58:29
Original
1186 people have browsed it

If you are very familiar with prototype objects and prototype attributes in JS, it will be very easy to understand the prototype chain and inheritance later. I would like to share my understanding of it with you.

 1  function Person(){ 2 } 3 Person.prototype.name = "jingzi"; 4 Person.prototype.age = 20; 5 Person.prototype.sayName = function(){ 6    alert(this.name); 
 7 }; 8  9 var person1 = new Person();10 person1.sayName();    //"jingzi"
Copy after login

This is The code of objects created using prototype mode is very short and not difficult to understand. If you have any doubts about prototype, please continue reading

Everyone, please ignore this ugly illustration (.・_・.)ノ. This is the key to understanding the problem~ Let’s get into the text. . . .

When each function is created, it will have a prototye attribute, which will point to the prototype object of the function. By default, each prototype object will get a constructor attribute, which contains a pointer to the function where the prototype attribute is located.

As shown above, when a Person function is created, it will have a prototype attribute, which points to the Person Prototype prototype object, and this prototype object has a constructor attribute, whose pointer points to Person, that is, prototype The function Person where the attribute is located. When you create an object instance, it will have a prototype attribute (because every function will have a prototype attribute when it is created \(^o^)/). This prototype attribute will point to its prototype object rather than directly to its constructor Person.

You need to remember here that the instance object gets in touch with the constructor through the prototype object.

Related recommendations:

Detailed explanation of JS prototype

##Recommended 7 articles about js prototype chain

A brief discussion on JS prototype objects and prototype chains_javascript skills

The above is the detailed content of Detailed introduction to js prototype. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!