> For the complete documentation index, see [llms.txt](https://shenjunhong.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shenjunhong.gitbook.io/blog/yuan-ma/yuan-ma-shou-xie-xi-lie/shou-xie-yi-ge-ji-sheng-zu-he-shi-ji-cheng.md).

# 手写一个寄生组合式继承

```js
// 寄生组合式继承
// 第一步:创建父类型原型的一个副本
// 第二步:为创建的副本添加 constructor 属性, 从而弥补因重写原型而失去的默认的 constructor 属性
// 第三步:将新创建的对象(即副本)赋值给子类型的原型
function inheritPrototype(Child, Parent) {
  // 继承原型上的属性
  Child.prototype = Object.create(Parent.prototype);

  // 修复 constructor
  Child.prototype.constructor = Child;
}
```
