首頁  >  文章  >  web前端  >  關於JavaScript繼承的多種方式以及優缺點的詳細介紹

關於JavaScript繼承的多種方式以及優缺點的詳細介紹

黄舟
黄舟原創
2017-05-14 10:12:021200瀏覽

這篇文章主要介紹了深入理解JavaScript繼承的多種方式和優缺點,具有一定的參考價值,有興趣的小伙伴們可以參考一下

寫在前面

本文講解JavaScript各種繼承方式和優缺點。

注意:

跟《JavaScript深入之建立物件》一樣,更像是筆記。

哎,再讓我感嘆一句:《JavaScript高級程式設計》寫得真是太好了!

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

問題:

1.引用類型的屬性被所有實例共享,舉個例子:

function Parent () {
  this.names = ['kevin', 'daisy'];
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy", "yayu"]

2.在建立Child 的實例時,不能向Parent傳參

2.借用建構子 (經典繼承)

function Parent () {
  this.names = ['kevin', 'daisy'];
}

function Child () {
  Parent.call(this);
}

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy"]

優點:

1.避免了引用類型的屬性被所有實例共享

2.可以在Child 中向Parent 傳參

舉例:

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

function Child (name) {
  Parent.call(this, name);
}

var child1 = new Child('kevin');

console.log(child1.name); // kevin

var child2 = new Child('daisy');

console.log(child2.name); // daisy

缺點:

方法都會在建構子中定義,每次建立實例都會建立一遍方法。

3.組合繼承

原型鏈繼承與經典繼承雙劍合璧。

function Parent (name) {
  this.name = name;
  this.colors = ['red', 'blue', 'green'];
}

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

function Child (name, age) {

  Parent.call(this, name);
  
  this.age = age;

}

Child.prototype = new Parent();

var child1 = new Child('kevin', '18');

child1.colors.push('black');

console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child('daisy', '20');

console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

優點:融合原型鏈繼承與建構子的優點,是 JavaScript 中最常用的繼承模式。

4.原型式繼承

function createObj(o) {
  function F(){}
  F.prototype = o;
  return new F();
}

就是ES5 Object.create 的模擬實現,將傳入的物件作為創建的物件的原型。

缺點:

包含引用類型的屬性值總是會共用對應的值,這點跟原型鏈繼承一樣。

var person = {
  name: 'kevin',
  friends: ['daisy', 'kelly']
}

var person1 = createObj(person);
var person2 = createObj(person);

person1.name = 'person1';
console.log(person2.name); // kevin

person1.firends.push('taylor');
console.log(person2.friends); // ["daisy", "kelly", "taylor"]

注意:修改person1.name的值,person2.name的值並未改變,並不是因為person1person2有獨立的name 值,而是因為person1.name = 'person1',給person1增加了name 值,並非修改了原型上的name 值。

5. 寄生式繼承

建立一個僅用於封裝繼承過程的函數,該函數在內部以某種形式來做增強對象,最後返回對象。

function createObj (o) {
  var clone = object.create(o);
  clone.sayName = function () {
    console.log('hi');
  }
  return clone;
}

缺點:跟借用建構函式模式一樣,每次建立物件都會建立一遍方法。

6. 寄生組合式繼承

為了方便大家閱讀,在這裡重複一下組合繼承的程式碼:

function Parent (name) {
  this.name = name;
  this.colors = ['red', 'blue', 'green'];
}

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

function Child (name, age) {
  Parent.call(this, name);
  this.age = age;
}

Child.prototype = new Parent();

var child1 = new Child('kevin', '18');

console.log(child1)

組合繼承最大的缺點是會呼叫兩次父建構函數。

一次是設定子類型實例的原型的時候:

Child.prototype = new Parent();

一次在創建子類型實例的時候:

var child1 = new Child('kevin', '18');

回想下new 的模擬實現,其實在這句中,我們會執行:

Parent.call(this, name);

在這裡,我們又會呼叫了一次Parent 建構函式。

所以,在這個範例中,如果我們印出child1 對象,我們會發現Child.prototype 和child1 都有一個屬性為colors,屬性值為['red', 'blue', 'green'] 。

那我們該如何精益求精,避免這次重複呼叫呢?

如果我們不使用 Child.prototype = new Parent() ,而是間接的讓 Child.prototype 存取到 Parent.prototype 呢?

看看如何實現:

function Parent (name) {
  this.name = name;
  this.colors = ['red', 'blue', 'green'];
}

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

function Child (name, age) {
  Parent.call(this, name);
  this.age = age;
}

// 关键的三步
var F = function () {};

F.prototype = Parent.prototype;

Child.prototype = new F();


var child1 = new Child('kevin', '18');

console.log(child1);

最後我們封裝這個繼承方法:

function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}

function prototype(child, parent) {
  var prototype = object(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}

// 当我们使用的时候:
prototype(Child, Parent);

引用《JavaScript高級程式設計》中對寄生組合式繼承的誇讚就是:

這種方式的高效率體現它只呼叫了一次Parent建構函數,並且因此避免了在Parent.prototype 上面創建不必要的、多餘的屬性。同時,原型鏈還能維持不變;因此,也能夠正常使用 instanceof 和 isPrototypeOf。開發者普遍認為寄生組合式繼承是引用型別最理想的繼承範式。

以上是關於JavaScript繼承的多種方式以及優缺點的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn