当前位置:网站首页>[prototype and prototype chain] get to know prototype and prototype chain~
[prototype and prototype chain] get to know prototype and prototype chain~
2022-07-28 03:42:00 【Bukaisen, East Africa】
Prototype and prototype chain are js One of the difficult points to understand , But as long as you understand it carefully , There will be gains
Before introducing the prototype and prototype chain , Let's understand the following questions first
- __proto__ and constructor The attribute is object Unique ;
- prototype The attribute is function Unique , Because functions are also objects , So functions also have __proto__ and constructor attribute .
- Attribute function :
- __proto__ The function of attribute is to access the attribute of an object , If this property does not exist inside the object , Then I'll go to it __proto__ The object the property points to ( parent object ) Look inside , Keep looking for , until __proto__ End of attribute null, Then return undefined, To look up is to look up null Top value , Will report a mistake . adopt __proto__ The link that attributes connect objects is what we call the prototype chain .
- prototype The function of attribute is to let the objects instantiated by this function find common attributes and methods , namely f1.proto === Foo.prototype.
- constructor Property means the constructor that points to the object , All functions ( At this point as the object ) The final constructors all point to Function.
1. Prototype
- proto: Implicit prototype
- prototype: Explicit prototypes
- Objects have implicit prototypes __proto__.
- The function has a display prototype prototype.
- Object's __proto__ To its constructor prototype.
- all Reference type There is one. __proto__( Implicit prototype ) attribute , Property value is a normal object
- all function There is one. prototype( Prototype ) attribute , Property value is a normal object
- all reference-type __proto__ attribute To its constructor prototype
Let's first understand three concepts :
- proto
every last object There is one. __proto__ attribute , He points to the prototype of the object
function Person(){
}
var person1 = new Person()
console.log(person1.__proto__ === Person.prototype) // true

- prototype
every last function There is one. prototype attribute , This property points to the prototype object of the function
function Person(){
}
Person.prototype.name = "protoName"
var person1 = new Person()
console.log(person1.name) // protoName
explain :
- person1 There is no such thing as name This attribute , So it will go to constructor Person look for , and Person The function itself does not name attribute , therefore person1 Will go Person.prototype Look up , That is to say Person On the prototype of

- constructor
Each prototype has a constructor attribute , The constructor pointing to the Association .
from constructor In terms of attributes , Only prototype Object has this attribute , And other objects constructor Attributes are all through __proto__ Objects from prototype inherited
function Person(){
}
var person1 = new Person()
console.log(Person.prototype.constructor === Person) // true
console.log(Person.prototype.constructor === person1.constructor) // true
explain :
- person1 There is no such thing as constructor attribute , So I will follow the prototype chain to find , Go to its constructor Person Search above , The prototype of constructor has constructor attribute

When the function is created ,JS A corresponding... Is created for this function prototype object , And this prototype Object's constructor Property in turn points to the function , namely Foo.prototype.constructor === Foo.
- The advantages of prototypes :
All properties and methods on the prototype object , Can be shared by the instance object created by the corresponding constructor *( This is it. JavaScript The basic design of inheritance mechanism ), in other words , You don't have to define the object instance information in the constructor , Instead, you can add this information directly to the prototype object .
Each constructor has one prototype( Prototype ) attribute , This attribute is the prototype object of the instance object created using the constructor .
2. Prototype chain
Prototype chains are used for Build new types of objects based on existing objects . It is similar to inheritance in class based languages .
Prototype on object instance It can be done by Object.getPrototypeOf(object) or proto attribute get , and Prototype on constructor It can be done by Object.prototype get .
Prototype chain features :
- When reading a property of an object ,JavaScript The engine first looks for the properties of the object itself , If you can't find it , Go to its prototype , If I still can't find it , Go to the prototype . If you go up to the top Object.prototype Still can't find , Then return to undefined.
- If the object itself and its prototype , All define an attribute with the same name , Then the priority is to read the properties of the object itself , It's called “ Cover ”(overriding
Nearby principle
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype.address = function(){
console.log(" Address ");
}
var xiaoming= new Person(" Xiao Ming ");
xiaoming.address();
- explain :
Constructors Person adopt prototype Point to Person Prototype object prototype
Person Prototype object prototype Through its own construtor Property refers back to the constructor Star
Person Created an object instance xiaoming,xiaoming Through its own __proto__ Property points to its prototype object
- Be careful :
One step up , Look for an attribute in the prototype chain , It has an impact on performance . The properties you are looking for are prototypes on the higher level , The greater the impact on performance . If you look for an attribute that doesn't exist , Will traverse the entire prototype chain
There will be deeper and better understood explanations in the future , I will slowly add
边栏推荐
- Tungsten Fabric SDN — BGP as a Service
- Unity simply implements the dialog function
- pip-script. py‘ is not present Verifying transaction: failed
- 【力扣】1337.矩阵中战斗力最弱的k行
- Push chart written by helm to harbor warehouse
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- 过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
- Airiot Q & A issue 6 | how to use the secondary development engine?
- 一篇文章掌握Postgresql中对于日期类数据的计算和处理
- LightPicture – 精致图床系统
猜你喜欢

Qt:QMessageBox消息框、自定义信号和槽

8000字讲透OBSA原理与应用实践

Swift中的协议
D2dengine edible tutorial (4) -- draw text

AIRIOT答疑第6期|如何使用二次开发引擎?

An article grasps the calculation and processing of date data in PostgreSQL

4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits

Unity简单实现对话功能

沃尔沃:深入人心的“安全感” 究竟靠的是什么?

695. Maximum area of the island
随机推荐
Leetcode skimming: dynamic programming 08 (segmentation and subsets)
How to uninstall clean ZABBIX service? (super detailed)
After 95, Alibaba P7 published the payroll: it's really heartbreaking
递归和非递归分别实现求第n个斐波那契数
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
MangoPapa 的实用小脚本(目录篇)
Collection | 0 basic open source data visualization platform flyfish large screen development guide
WordPress简约mkBlog博客主题模板v2.1
【P4】解决本地文件修改与库文件间的冲突问题
一个仿win10蓝屏的404页面源码
BRD,MRD,PRD的区别
Differences among BRD, MRD and PRD
【OPENVX】对象基本使用之vx_image
【P4】 查看库文件两个历史版本的区别
[openvx] VX for basic use of objects_ convolution
[openvx] VX for basic use of objects_ image
高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
pip-script. py‘ is not present Verifying transaction: failed
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置