为什么会有借用构造函数继承模式

function People(name, sex, phone) {
  this.name = name;
  this.sex = sex;
  this.phone = phone;
}

People.prototype.doEat = function () {
  console.log(this.name + '吃饭');
}

function ChinesePeople(name, sex, phone, national) {
  // 调用父类时修改 this 并传递参数
  People.apply(this, [name, sex, phone]);
  this.national = national
}
let chinesePeople = new ChinesePeople('xiebenyin', 'man', 110, 'chinese')
console.log(chinesePeople.phone);
  • 借用构造函数继承模式 解决原型链继承时无法传递参数给父类
  • 缺点是 ChinesePeople 不能使用 People 的原型
posted @ 2021-12-13 22:48  霸哥yyds  阅读(25)  评论(0)    收藏  举报