Home >Web Front-end >JS Tutorial >Detailed introduction to js prototype

Detailed introduction to js prototype

小云云
小云云Original
2018-03-06 13:58:291225browse

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"

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!

Statement:
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