组合继承,可以继承父类的还可以自己添加自己的属性,如Son.age属性
function Father(name) {
this.name = name
this.hbody = ['篮球', '足球', '兵乒球']
}
Father.prototype.getName = function() {
console.log(this.name)
}
function Son(name, age) {
Father.call(this, name)
this.age = age
}
Son.prototype = new Father()
// 手动挂上构造器,指向自己的构造函数
Son.prototype.constructor = Son
let S = new Son('小明', 18)
let F = new Father('安其拉')
console.log('组合继承1', S)
console.log('组合继承2', F)