如何实现寄生组合式继承:
//第一步:子类继承父类的属性
function children(grade) { //children为子类
this.grade = grade //此处设置grade为自身属性,grade参数为值,
Person.apply(this,arguments) //Person为父类,修改父类的this为children的this,arguments为父类需要的参数
}
//第二步:第一种方式:
children.prototype = new Person() //这是继承的一种方式,但是此方式会继承很多来自父类的其他属性,但子类并不需要,会加重子类负担,个人看情况设置
//第二步:第二种方式:
function inheritPrototype(){
function F() {} //创建一个空的方法
F.prototype = Person.prototype //用这个空的方法F()的prototype来继承父类的prototype
children.prototype = new F() //这样children就只继承到父类的prototype下的属性
children.prototype.constructor = children //找回constructor
}
inheritPrototype(children,Person) //调用此方法得到子类children