当前位置:网站首页>原型链继承
原型链继承
2022-07-06 09:29:00 【社会你磊哥,命硬不弯腰】
在原型链跌了好几次跟头,今天将其归类稍加总结一下。在js中,对象都有__proto__属性,一般这个是被称为隐式的原型,该隐式原型指向构造该对象的构造函数的原型。在js中,一切皆对象,对象则可通过构造函数和字面量的形式生成。
首先我们要扒一扒proto、prototype、constructor
__proto__
:事实上就是原型链指针!真正指向了祖先级对象。
prototype
:这个是指向构造函数的原型对象,其实原型对象就只是个普通对象,里面存放着所有实例对象需要共享的属性和方法!
constructor
:每一个原型对象都包含一个指向构造函数的指针,就是constructor
在这里有一个小技巧帮助我们判断,当求一个对象__proto__是谁的时候,我们首先要想他是那个构造函数new出来的,它指向的就是此构造函数的prototype原型对象。还有就是哪个构造函数要求prototype,比如Array.prototype
是等于[].__proto__
的,还有就是函数也是new Function()
出来的对象。
下面这个图可以帮助我们很好的理解原型链的整个知识点
// Foo.__proto__因为是通过new Function()创建,所以指向Function.prototype
// Foo.prototype指向自己的原型对象,和foo.__proto__相同
// Object,Array,Function都是通过Function构造函数new出来的,所以其__proto__指向Function.prototype
// Function.prototype => Function.prototype.__proto__ = Object.prototype => Object.prototype.__proto__ = null
//底下的运行结果都是true
console.dir(Foo.__proto__ == Function.prototype);
console.dir(Foo.__proto__.__proto__ == Object.prototype);
console.dir(Function.prototype.__proto__ == Object.prototype)
console.dir(Foo.prototype == foo.__proto__);
console.dir(Foo.__proto__.__proto__ == obj.__proto__);
console.dir(Function.prototype == Object.__proto__)
console.dir(Array.__proto__ == Function.prototype);
console.dir(Function.__proto__ == Function.prototype);
console.dir(foo.__proto__.__proto__ == Object.prototype)
附带几个面试题与君共赏
题目一
var F = function () {
}
Object.prototype.a = function () {
}
Function.prototype.b = function () {
}
var f = new F()
方法f只能调用a,不能到用b,F可以调用a方法和b方法,因为f.__proto__
并没有经过Function.prototype
这条链路
题目二
function Parent(){
this.a = 'Parent'
}
function Tom() {
this.a = 'Tom'
}
Parent.__proto__.print = function(){
console.log(this.a)
}
Parent.print()
Tom.print()
var child = new Parent()
child.print()
// undefined
// undefined
// Uncaught TypeError: child.print is not a function
Parent
和Tom
都是Function
的实例,因此相当于在Function.prototype
上面挂载了一个print
方法,因此Parent.print()
可以调用到这个方法,但是没有返回值,一个原因是 Parent
方法没有执行,一个是因为this此时指向的是Parent
,Parent
方法上没有a
属性,而child本身是基于构造函数创建了一个对象,child.__proto__.__proto__ === Object.prototype
,因此在其原型链上找不到print
方法。
边栏推荐
- 第7章 __consumer_offsets topic
- 这116名学生,用3天时间复刻了字节跳动内部真实技术项目
- 姚班智班齐上阵,竞赛高手聚一堂,这是什么神仙编程大赛?
- JS encapsulates the method of array inversion -- Feng Hao's blog
- 7-5 blessing arrived
- ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
- LeetCode 1637. The widest vertical area between two points without any point
- Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
- Monomer application concept
- Eureka single machine construction
猜你喜欢
Chapter 1 overview of MapReduce
7-4 harmonic average
Spark independent cluster dynamic online and offline worker node
字节跳动技术面试官现身说法:我最想pick什么样的候选人
Hbuilder x format shortcut key settings
7-10 punch in strategy
LeetCode 1584. Minimum cost of connecting all points
Chapter III principles of MapReduce framework
第五章 Yarn资源调度器
100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
随机推荐
Audio and video development interview questions
DS18B20数字温度计系统设计
【锟斤拷】的故事:谈谈汉字编码和常用字符集
LeetCode 1557. The minimum number of points that can reach all points
ByteDance new programmer's growth secret: those glittering treasures mentors
~83 form introduction
Jedis
Introduction to microservices
MP4格式详解
Chapter 5 namenode and secondarynamenode
LeetCode 1558. Get the minimum number of function calls of the target array
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
LeetCode 1636. Sort the array in ascending order by frequency
Simple records of business system migration from Oracle to opengauss database
TypeScript基本操作
Cartesian tree (modified)
Chapter 2 shell operation of hfds
7-10 punch in strategy
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
Chapter 7__ consumer_ offsets topic