Skip to content

Latest commit

 

History

History
82 lines (39 loc) · 1.18 KB

继承.md

File metadata and controls

82 lines (39 loc) · 1.18 KB

关于对象继承的几个方法

1.经典继承

利用原型链直接在对象的原型上对添加方法和属性,并让子类的原型等于父类的构造函数来实现继承

例子:

function Subfather(){

this.name="lisi"

}

Subfather.proptype.getname(){

return this. name

}

function Subson(){

}

Subson.proptype=new Sunfather()

2.构造继承

借用构造函数实现来实现继承,即利用apply和call实现对函数的继承,因为构造函数方法都在函数内部,无法达到函数服用的 目的,例如:

function Subfather(){

this.name='lisi'

}

function Subsonr(){

this.call(Subfather)

}

const son=new Subson();

console.log(son.name)//'lisi'

3.寄生组合继承

function Subfather(){

this.name="lisi"

}

Subfather.proptype.getname(){

return this. name

}

function Subson(age,name){

this.age=age;

Subfather.call(this,name)

}

Subson.proptype=new Subfather();

Subson.proptype.constructor=Subson;

Subson.proptype.getage=function(){

console.log(this.name)

}