function father(value){
this.value=value;
}
father.prototype.getValue=function(){
console.log(this.value)
}
function child(value){
father.call(this,value)
}
child.prototype=new father;
var childOne=new child(1);
childOne.getValue();
//组合继承,缺点将所有父级的属性全部继承到子类的身上造成不必要的浪费
function parent(value){
this.value=value;
}
parent.prototype.getValue=function(){
console.log(this.value)
}
function childL(value){
father.call(this,value)
}
childL.prototype=Object.create(parent.prototype,{
constructor:{
value:child,
enumerable:false,
configurable:true,
writable:true
}
})
var childTwo=new childL(2);
childTwo.getValue();
//组合寄生集成该继承方式与上面的方法不同,使用create创制造一个新的对象,集成父级原型对象。
class Parent{
constructor(value){
this.value=value
}
getValue(){
console.log(this.value)
}
}
class Child extends Parent{
constructor(value){
super(value)
this.value=value;
}
}
let childThree=new Child(3);
childThree.getValue()
//ES6继承方式,这种方式使用class这个语法糖,class的本质还是一个函数,super的作用在继承父类的时候调用该方法可以改变this的指向,相当于Parent.call(this,value)