JavaScript深入之继承的多种方式和优缺点

2020-08-18  本文已影响0人  JAZI

https://juejin.im/post/6844903477819211784
1.原型链继承

function Parent () {
    this.name = 'kevin';
}

Parent.prototype.getName = function () {
    console.log(this.name);
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

console.log(child1.getName()) // kevin

缺点:

2.借用构造函数(经典继承)
缺点:
方法都在构造函数中定义,每次创建实例都会创建一遍方法。


image.png
image.png
image.png
image.png
  1. 寄生组合式继承


    image.png
    image.png
    image.png
上一篇下一篇

猜你喜欢

热点阅读