原理:

在子类里面通过调用父类、等同于子类里面把父类复制过来了

代码理解

  1. // 父类
  2. function SuperType(){
  3. this.name = 'p';
  4. }
  5. SuperType.prototype.getName = function() {
  6. return this.name;
  7. }
  8. // 子类
  9. function SubType(){
  10. this.age = 18;
  11. SuperType.call(this)
  12. }
  13. SubType.prototype.getAge = function() {
  14. return this.name;
  15. }
  16. let instance = new SubType();
  17. console.log(instance)
  18. // { age: 18, name: "p" }

缺点

  • 无法实现复用、每次去构建实例都需要重新去执行下父类
  • 无法继承父类的原型方法