class讲解之11 prototype 、__proto__、
2020-08-12 本文已影响0人
zhang463291046
以下内容是引用或者借鉴别人的,自己只是做个笔记,方便学习。理解错误的地方,欢迎评论。如有侵权,私聊我删除,未经允许,不准作为商业用途
实例与类之间的那点关系
class A {
constructor() {
this.x = 1;
}
print() {
}
}
let a = new A()
console.log(a.__proto__ === A.prototype) // true
console.log(A === A.prototype.constructor) // true
图解

类与类之间的那点关系
class B {
constructor() {
this.x = 2;
}
printB() {
}
}
class A extends B {
constructor() {
super();
this.x = 1;
}
print() {
}
}
console.log(A.__proto__ === B); // true
console.log(A.prototype.__proto__ === B.prototype); // true
图解

实例与类、类与类的之间那点关系,最终形成原型链
class B {
constructor() {
this.x = 2;
}
printB() {
}
}
class A extends B {
constructor() {
super();
this.x = 1;
}
print() {
}
}
let a = new A();
console.log(a.__proto__ === A.prototype) // true
console.log(a.__proto__.__proto__ === B.prototype) // true
console.log(a.__proto__.__proto__.__proto__ === Object.prototype) // true
图解
