当前位置:网站首页>一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
2022-07-02 11:21:00 【名字还没想好*】
一、先上图

二、prototype、proto、constructor规则
规则一:实例没有prototype,实例的constructor指向构造函数,实例的__proto__指向上一层的原型(也就是构造函数的prototype)。
规则二 :Object、构造函数、Function都是Function的实例。
实例:结合这两条规则,图中的f1是Foo的实例,Foo、Object、Function均为Function的实例。指向就非常明确了。
规则三:函数的原型的__proto__指向上一层的原型(也就是Fun.prototype.proto 指向Object.prototype)
规则四:函数的原型的constructor指向函数本身(可以理解为prototype和constructor抵消了)
实例:结合三四规则,下图的也走通了。
三、上代码
function Fun(){
}
const f1 = new Fun()
console.log(f1.constructor === Fun)
console.log(f1.__proto__ === Fun.prototype)
console.log(Function.prototype === Function.__proto__)
console.log(Fun.prototype.constructor === Fun)
console.log(Fun.prototype.__proto__ === Function.__proto__.__proto__)
console.log(Fun.constructor.__proto__ === Function.prototype)
console.log(Object.constructor === Function)
console.log(Function.constructor === Function)
console.log(Function.prototype.__proto__ === Object.prototype)
console.log(Object.prototype.__proto__ === null)
//结果全部为true
总结:当图中的关系已经印在脑海中的时候,可以尝试自己写出三者之间的转换等式,以及各种嵌套。
例如:new Fun().constructor.proto.proto === Object.prototype
边栏推荐
- Start to write a small demo - three piece chess
- Stm32-dac Experiment & high frequency DAC output test
- 快解析:轻松实现共享上网
- threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
- Openharmony notes --------- (4)
- Yolov3 & yolov5 output result description
- 路由(二)
- How kaggle uses utility script
- uni-app中使用computed解决了tab切换中data()值显示的异常
- Essential elements of science fiction 3D scenes - City
猜你喜欢

什么是 eRDMA?丨科普漫画图解

Fabric.js 缩放画布

跨服务器数据访问的创建链接服务器方法
![[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)](/img/08/9f25515e600a3174340b2589c81b0e.jpg)
[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)

What is erdma? Popular science cartoon illustration
![[to be continued] [UE4 notes] l5ue4 model import](/img/6b/d3083afc969043dbef1aeb4fccfc99.jpg)
[to be continued] [UE4 notes] l5ue4 model import

Essential elements of science fiction 3D scenes - City

Teamtalk source code analysis win client

How kaggle uses utility script

uniapp自动化测试学习
随机推荐
STM32标准固件库函数名记忆(二)
路由(二)
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
TeamTalk源码分析之win-client
3、函数指针和指针函数
Fabric. JS dynamically set font size
Start to write a small demo - three piece chess
4、数组指针和指针数组
线性dp求解 最长子序列 —— 小题三则
Thymeleaf dependency
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
Default slot, named slot, scope slot
2、const 型指针
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
Federated Search: all requirements in search
mongodb的认识
HMS core machine learning service helps zaful users to shop conveniently
数据湖(十一):Iceberg表数据组织与查询
Custom events, global event bus, message subscription and publishing, $nexttick
2. Const pointer

