当前位置:网站首页>一张图彻底掌握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
边栏推荐
- Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
- Qt原代码基本知识
- Daily learning 3
- Default slot, named slot, scope slot
- Data consistency between redis and database
- Fabric.js 上划线、中划线(删除线)、下划线
- Pychart connects to the remote server
- Fabric. JS dynamically set font size
- uni-app中使用computed解决了tab切换中data()值显示的异常
- PTA question bank== > complex four operations, one for one, examination seat number (7-73)
猜你喜欢
Codeforces Round #803 (Div. 2)(A~D)
Pycharm连接远程服务器
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
快解析:轻松实现共享上网
Do you know that there is an upper limit on the size of Oracle data files?
Qt新建项目
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
随机推荐
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Fabric.js 自由绘制椭圆
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
复用和分用
数据湖(十一):Iceberg表数据组织与查询
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
STM32-DAC实验&高频DAC输出测试
2、const 型指针
What is erdma? Popular science cartoon illustration
什么是 eRDMA?丨科普漫画图解
P1042 [noip2003 popularization group] Table Tennis
Pycharm连接远程服务器
HMS core machine learning service helps zaful users to shop conveniently
Basic knowledge of QT original code
kaggle如何使用utility script
Stm32-dac Experiment & high frequency DAC output test
Threejs controller cube space basic controller + inertia control + flight control
Advanced usage of C language -- function pointer: callback function; Conversion table