es5实现中,每个对象都有__proto__属性(也就是关系图中[[prototype]]属性),指向对应的构造函数的prototype。Class 作为构造函数的语法糖,同时有prototype属性和__proto__属性,因此同时存在两条继承链。
子类的__proto__属性,表示构造函数的继承,总是指向父类子类prototype属性的__proto__属性,表示方法的继承,总是指向父类的prototype。
class A{
constructor(){
this.name = 'A';
this.x = 1;
}
sayName(){
console.log(this.name);
}
};
class B extends A{
constructor(){
super();
this.name = 'B';
this.y = 1;
}
printY(){
console.log(this.y);
}
}
var a = new A();
var b = new B();
关系图:
转载于:https://www.cnblogs.com/bluey/p/9771387.html