当前位置:网站首页>Prototype chain inheritance
Prototype chain inheritance
2022-07-06 17:04:00 【Society, you Lei brother, life is hard, don't bend down】
Fell several times in the prototype chain , Today, I will classify it and summarize it a little . stay js in , Object has __proto__ attribute , Generally, this is called implicit prototype , The implicit prototype points to the prototype that constructs the object's constructor . stay js in , Everything is the object , Objects can be generated in the form of constructors and literals .
First of all, let's have a steak proto、prototype、constructor
__proto__
: In fact, it is the prototype chain pointer ! It really points to the ancestor level object .
prototype
: This is the prototype object that points to the constructor , In fact, the prototype object is just an ordinary object , It stores the properties and methods that all instance objects need to share !
constructor
: Each prototype object contains a pointer to the constructor , Namely constructor
Here is a tip to help us judge , When seeking an object __proto__ Who was it when , First of all, we need to think about the constructor new Coming out , It points to this constructor prototype Prototype object . There is also a constructor that requires prototype, such as Array.prototype
Is equal to [].__proto__
Of , There is also function new Function()
Out object .
The following figure can help us understand the whole knowledge points of the prototype chain
// Foo.__proto__ Because it's through new Function() establish , So the point to Function.prototype
// Foo.prototype Point to your prototype object , and foo.__proto__ identical
// Object,Array,Function It's all through Function Constructors new Coming out , So its __proto__ Point to Function.prototype
// Function.prototype => Function.prototype.__proto__ = Object.prototype => Object.prototype.__proto__ = null
// The running results below are 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)
Attach several interview questions to share with you
Topic 1
var F = function () {
}
Object.prototype.a = function () {
}
Function.prototype.b = function () {
}
var f = new F()
Method f Can only call a, Unable to use b,F You can call a Methods and b Method , because f.__proto__
No way Function.prototype
This link
Topic two
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
and Tom
All are Function
Example , Equivalent to... Therefore Function.prototype
There is a print
Method , therefore Parent.print()
You can call this method , But there is no return value , One reason is that Parent
Method not implemented , One is because this At this point, it points to Parent
,Parent
There's no way a
attribute , and child It creates an object based on the constructor ,child.__proto__.__proto__ === Object.prototype
, Therefore, it cannot be found on its prototype chain print
Method .
边栏推荐
- Error occurred during initialization of VM Could not reserve enough space for object heap
- Activit零零碎碎要人命的坑
- In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
- 程序员定位解决问题方法论
- Von Neumann architecture
- Activit fragmented deadly pit
- 登陆验证koa-passport中间件的简单使用
- ~82 style of table
- 搭建flutter环境入坑集合
- 7-12 inventory code base
猜你喜欢
Activit零零碎碎要人命的坑
谢邀,人在工区,刚交代码,在下字节跳动实习生
~68 Icon Font introduction
Install docker under windows10 (through Oracle VM VirtualBox)
The 116 students spent three days reproducing the ByteDance internal real technology project
Activiti目录(五)驳回、重新发起、取消流程
原型链继承
~69 other ways to use icon fonts
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
arithmetic operation
随机推荐
Activiti directory (III) deployment process and initiation process
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
7-12 inventory code base
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
一个数10年工作经验的微服务架构老师的简历
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
Activiti目录(三)部署流程、发起流程
~81 long table
Activiti目录(五)驳回、重新发起、取消流程
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
koa中间件
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
8086 CPU internal structure
How to configure hosts when setting up Eureka
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
服务器端渲染(SSR)和客户端渲染(CSR)的区别
Shell_ 03_ environment variable
Koa Middleware
算数运算指令
Shell_ 04_ Shell script