当前位置:网站首页>Prototype and prototype chain in JS
Prototype and prototype chain in JS
2022-07-01 02:41:00 【Love front-end Maomao】
JS The archetype of and Prototype chain It is a hot topic for front-end interview questions , Basically, the probability of meeting is very high !
First , What is a prototype ?
every last javascript object ( except null Outside ) At the time of creation , Will be associated with another object , This object is what we call prototype , Every object comes from the prototype “ Inherit ” attribute .
for instance :
function Person(age) {
this.age = age
}
Person.prototype.name = 'kavin'
var person1 = new Person()
console.log(person1.name) //kavinThat is to say, through Constructors When creating an instance , If you look for an attribute value in an instance , If he has one, he will output it directly , If not , Will go to its prototype object to find .

prototype: Each function has one prototype attribute , This property points to the prototype object of the function .
__proto__: This is every object ( except null Outside ) There will be properties , be called __proto__, This property points to the prototype of the object .
constructor: Each prototype has a constructor attribute , The constructor pointing to the Association .
The knowledge test sites related to prototypes are generally around this picture , So how to verify the correctness of this diagram ?
console.log(Person===Person.prototype.constructor) // true
console.log(person.__proto__ == Person.prototype) // true
console.log(person.__proto__.constructor == Person) // true
console.log(person.constructor == Person) // trueThen what is the prototype chain ?
Simply review the constructor 、 Relationship between prototype and instance : Each constructor has a prototype object , The prototype object contains a pointer to the constructor , Instances contain an internal pointer to the prototype object . So if we make the prototype object equal to an instance of another type , What will happen ? obviously , The prototype object at this point will contain a pointer to another prototype , Accordingly , Another prototype also contains a pointer to another constructor . If another prototype is another instance of another type , So the above relationship still holds . And so on , It forms a chain of instances and prototypes .
The simple expression is : As mentioned above, if the instance does not have a property , It will go to its prototype to find , But what if it doesn't have this attribute in its prototype , Will you stop looking , not always , Because its prototype may also have its own prototype , At this time, he will go to the prototype of its prototype to find , Will it stop at this time , Not necessarily , It depends on whether his prototype has a prototype , In this way, a prototype chain is formed .
Until the last prototype cannot be found, return null

边栏推荐
- Pycharm 打开远程目录 Remote Host
- Dell服务器重启iDRAC方法
- Leetcode (524) -- match the longest word in the dictionary by deleting letters
- Zero foundation self-study SQL course | window function
- ipmitool下载地址和编译安装时可能出现的问题
- 鼠标悬停效果五
- SWT / anr problem - SWT caused by too long dump time
- SWT / anr problem - binder stuck
- I want to know how to open a stock account? Is it safe to open an account online?
- 使用ipmitool配置X86服务器的BMC网络和用户信息
猜你喜欢
随机推荐
Résumé des styles de développement d'applets Wechat
Clickhouse eliminates the gap caused by group by
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
[2022] Jiangxi postgraduate mathematical modeling scheme and code
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
go: finding module for package
[PR # 5 A] two way running (state pressure DP)
旷世轻量化网络ShuffulNetV2学习笔记
pycharm 软件deployment 灰色 无法点
Go import self built package
nacos配置中心使用教程
C # generates PPK files in putty format (supports passphrase)
SAP ALV summary is inconsistent with exported excel summary data
鼠标悬停效果三
js防抖和节流
【PR #5 A】双向奔赴(状压DP)
Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
The latest wechat iPad protocol code obtains official account authorization, etc
鼠标悬停效果九
RestCloud ETL WebService数据同步到本地

![[JS] [Nuggets] get people who are not followers](/img/cc/bc897cf3dc1dc57227dbcd8983cd06.png)







