答案:
function Parent (name, age) {
this.name = name
this.age = age
}
Parent.prototype.friends = ["xiaozhang", "xiaoli"]
Parent.prototype.eat = function () {
console.log(this.name + " 吃饭");
}
function Son (name, age, favor, sex) {
Parent.call(this, name, age)// TS继承中使用super
this.favor = favor // 兴趣爱好
this.sex = sex
}
Son.prototype = Parent.prototype// S101
// 给Son.prototype原型增加了方法,就等于给Parent.prototype增加了方法
Son.prototype.step = function () {
console.log(this.name, "爱好", this.favor);
}
let parent = new Parent("大山", 33);
parent.step();// 导致父对象也可以调用子类的step独有方法,这就不正确了,这就是为什么不能直接Son.prototype = Parent.prototype的原因
let sonobj = new Son("张三", 23, "打篮球", "男");
sonobj.step();