- JS中第一种继承方案:原型继承(让子类的原型等于父类的实例即可)
原型继承:
(1)父类中私有的和公有的属性方法,最后变为子类实例公有的
(2)和其他不同,原型继承并会把父类的属性方法“拷贝”给子类,而是让子类实例基于__proto__原型链找到自己定义的属性和方法( “指向/查找”方法)
<script>
function Parent(){
this.x=100;
}
Parent.prototype.getX=function getX(){
return this.x=x;
};
function Child(){
this.y=20;
}
Child.prototype=new Parent;
Child.prototype.getY=function getY(){
return this.y;
};
let c1=new Child;
console.log(c1);
</script>

- JS中第二种继承方式,CALL继承,只能继承父类私有的属性。
<script>
function Parent(){
this.x=100;
}
Parent.prototype.getX=function getX(){
return this.x=x;
};
function Child(){
Parent.call(this);
this.y=20;
}
Child.prototype.getY=function getY(){
return this.y;
};
let c1=new Child;
console.log(c1);
</script>
- JS中第三种继承,寄生组合式继承(CALL继承+另类原型继承)
<script>
function Parent(){
this.x=100;
}
Parent.prototype.getX=function getX(){
return this.x=x;
};
function Child(){
Parent.call(this);
this.y=20;
}
Child.prototype=Object.create(Parent.prototype);
Child.prototype.constructor=Child;
Child.prototype.getY=function getY(){
return this.y;
};
let c1=new Child;
console.log(c1);
</script>

- ES6中的类和继承,使用class,不能当作普通函数执行,只能new执行。
<script>
class Parent{
constructor(){
this.x=101;
}
getX(){
return this.x;
}
}
class Child extends Parent{
constructor(){
super();
this.y=20;
}
getY(){
return this.y;
}
}
</script>