当前位置:网站首页>原型链继承
原型链继承
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方法。
边栏推荐
- SQL快速入门
- Cartesian tree (modified)
- Educational Codeforces Round 122 (Rated for Div. 2)
- ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
- Jedis
- ~74 JD top navigation bar exercise
- LeetCode 1545. Find the k-th bit in the nth binary string
- ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
- Shell_ 01_ data processing
- Basic principles of video compression coding and audio compression coding
猜你喜欢

字节跳动技术面试官现身说法:我最想pick什么样的候选人

Eureka single machine construction

Chapter 5 namenode and secondarynamenode

ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021

Audio and video development interview questions

LeetCode 1552. Magnetic force between two balls

Shell_ 02_ Text three swordsman

ffmpeg命令行使用

J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets

was unable to send heartbeat
随机推荐
我走过最迷的路,是字节跳动程序员的脑回路
LeetCode 1557. The minimum number of points that can reach all points
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
Shell_ 04_ Shell script
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
LeetCode 1638. Count the number of substrings with only one character difference
~69 other ways to use icon fonts
~84 form supplement
LeetCode 1020. Number of enclaves
Chapter 5 namenode and secondarynamenode
Chapter 5 yarn resource scheduler
~75 background
图像处理一百题(11-20)
DS18B20数字温度计系统设计
Shell_ 05_ operator
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
Chapter 7__ consumer_ offsets topic
Solve the problem that intel12 generation core CPU single thread only runs on small cores
Solr word segmentation analysis
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia