当前位置:网站首页>Prototype chain analysis

Prototype chain analysis

2022-06-25 16:06:00 . calin.

What is prototype chain

Objects have prototype objects ( The prototype object is described below ), Prototype objects have prototype objects , It forms a chain structure , It's called the prototype chain . The figure below is a partial structure of the prototype chain ( You can customize the person Constructor looks like ).
person Constructor through new person() Create examples P1,p1 Can pass -proto- Access its prototype object , Constructors can be constructed by prototype Property to access the prototype object , Prototype objects can also be created by constructor Access the constructor .
As object , The prototype object also has its prototype object ( Because all objects have prototype objects ), So you can also pass -proto- Access his prototype object , Here it is. Object, and Object The prototype object of is null.
 Insert picture description here

The role of the prototype chain

For member lookup
When accessing members of an object , It is impossible to find whether the prototype object that will access it has this member
, Keep looking along the prototype chain , Know they're found , If it is not found, it will return undefined.

The role of prototype objects

stay ES5 front , Object - oriented programming is implemented through constructors and prototype objects , Using constructors has one drawback —— Objects created using constructors do not use the same method , It's a waste of memory . How to make these objects use the same method ? At this point, the prototype object will be used .
Every object can use prototype Property to access the prototype object , Members added to the prototype object can be accessed by the instance object created by the constructor .

原网站

版权声明
本文为[. calin.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190536014603.html