当前位置:网站首页>原型和原型链
原型和原型链
2022-08-10 08:42:00 【Oh No 发量又少了】
引用类型有如下四个规则:
规则一
引用类型,都具有对象特性,即可自由扩展属性:
const obj = {
}
const arr = []
const fn = function () {
}
obj.a = 1
arr.a = 1
fn.a = 1
console.log(obj.a) // 1
console.log(arr.a) // 1
console.log(fn.a) // 1
规则二
引用类型,都有一个隐式原型 __proto__
属性,属性值是一个普通的对象:
const obj = {
};
const arr = [];
const fn = function() {
}
console.log('obj.__proto__', obj.__proto__);
console.log('arr.__proto__', arr.__proto__);
console.log('fn.__proto__', fn.__proto__);
规则三
引用类型,隐式原型 __proto__
的属性值指向它的构造函数的显式原型 prototype
属性值:
const obj = {
};
const arr = [];
const fn = function() {
}
obj.__proto__ == Object.prototype // true
arr.__proto__ === Array.prototype // true
fn.__proto__ == Function.prototype // true
规则四
当你试图得到一个对象的某个属性时,如果这个对象本身没有这个属性,那么它会去它的隐式原型 __proto__
(也就是它的构造函数的显式原型 prototype
)中寻找:
const obj = {
a:1 }
obj.toString
// ƒ toString() { [native code] }
首先, obj
对象并没有 toString
属性,之所以能获取到 toString
属性,是遵循了第四条规则,从它的构造函数 Object
的 prototype
里去获取。
特例
function Person(name) {
this.name = name
return this // 其实这行可以不写,默认返回 this 对象
}
var nick = new Person("nick")
console.log(nick.toString());
//打印的结果为:[object Object]
这里就引出 原型链 的概念了, nick
实例先从自身出发检讨自己,发现并没有 toString
方法。找不到,就往上走,找 Person
构造函数的 prototype
属性,还是没找到。构造函数的 prototype
也是一个对象嘛,那对象的构造函数是 Object
,所以就找到了 Object.prototype
下的 toString
方法。
function Person(name) {
this.name = name
return this // 其实这行可以不写,默认返回 this 对象
}
var nick = new Person("nick")
console.log(Person.prototype);
console.log(Person.prototype.__proto__ === Object.prototype);
打印结果如下:
**注意:**当我们往对象的__proto__
属性中添加属性时,对象的__proto__
属性仍会指向构造函数的prototype
属性
function Person(name) {
this.name = name
return this // 其实这行可以不写,默认返回 this 对象
}
var nick = new Person("nick")
console.log('1',nick.__proto__ === Person.prototype);
nick.__proto__.age = 12;
console.log('2',nick.__proto__ === Person.prototype);
打印结果如下:
图片描述原型和原型链
总结
通过四个特性、一个例子、一张图片、一个方法,大家应该对原型和原型链的关系有了大概的认知。我的认知就是,原型链就是一个过程,原型是原型链这个过程中的一个单位,贯穿整个原型链。
边栏推荐
- Rust learning: 6.4_ enumeration of composite types
- I don't want to do accounting anymore, Die changed to a new one, moved forward bravely, and finally successfully passed the career change test to double his monthly salary~
- 刷题工具h
- 并查集模板
- 【OAuth2】十九、OpenID Connect 动态客户端注册
- 浅谈DAO+DeFi+NFT模式开发代码技术方案丨链游元宇宙NFT盲盒项目技术开发逻辑(源码程序)
- 二叉树 --- 堆
- ARM体系结构2:处理器内核和汇编指令集
- 不同的命令行风格
- 日期类(暑假每日一题 19)
猜你喜欢
ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
iwemeta metaverse: Ali's first COO: how to build a sales force
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
DAY25: Logic vulnerability recurrence
ARM结构体系3:ARM指令的寻址和异常中断处理
微信小程序--》小程序生命周期和WXS使用
【NeRF】原始论文解读
【业务架构】价值链分析:提高客户价值和盈利能力
Pieces of TensorFlow 2.9 (1)
[Learn Rust together | Advanced articles | RMQTT library] RMQTT message server - installation and cluster configuration
随机推荐
1-31部 1-31套 和硬件工程师90天学习资料及笔记汇总
ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
Binary tree --- heap
1499. 满足不等式的最大值 堆/双端队列
ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
day16--抓包工具Charles的使用
The sixteenth day & the basic operation of charles
【Unity入门计划】Collision2D类&Collider2D类
J9数字论:Web3.0+互联网电商会引起怎样的火花?
2 模块一:科研思维培养
ARM Architecture 2: Processor Core and Assembly Instruction Set
菜鸟、小白在autojs和冰狐智能辅助之间如何选择?
编程老手如何在autojs和冰狐智能辅助之间选择?
【一起学Rust | 进阶篇 | RMQTT库】RMQTT消息服务器——安装与集群配置
iwemeta metaverse: Ali's first COO: how to build a sales force
NPU architecture and force analysis
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
Process management (dynamic)
I don't want to do accounting anymore, Die changed to a new one, moved forward bravely, and finally successfully passed the career change test to double his monthly salary~