DIV CSS 佈局教程網

JavaScript 的繼承是怎麼回事:從原型鏈到 superclass 的三個階段
編輯:關於JavaScript     

第一階段:最直觀的寫法

function A(){
  this.funB = function(){
    alert('A:funB');
  };
}
A.prototype = {
  funA:function(){
    alert('A:funA');
  }
};

function B(){}

function extend(sub,parent){
  sub.prototype = new parent();
  sub.prototype.constructor = sub;
}

extend(B,A);

var b = new B();
b.funA(); // 'A:funA'
b.funB(); // 'A:funB'
alert(b instanceof A); // true

一眼就能看出在做什麼:定義 A、B 兩個建構函式,再用 extend 讓 B 繼承 A。原理是把父類別 new 出來指派給子類別的 prototype。

instanceof 也回傳 true,這不是巧合——要讓 instanceof 成立,兩個建構函式的 prototype 必須指向同一個物件(直接或間接皆可)。

但這個寫法有個問題。在一般的物件導向語言裡,子類別繼承父類別時不會觸發父類別的建構子;而這裡 new parent() 卻在繼承的那一刻就把父類別建構子執行掉了。

第二階段:用空函式避開建構

function A(){
  this.Astr = 'hello A';
}
A.prototype = {
  funA:function(){
    alert(this.Astr);
  }
};

function B(){
  arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
  this.Bstr = 'hello B';
}
B.prototype = {
  funB:function(){
    alert(this.Bstr);
  }
};

function C(){
  arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
  alert(this.Astr);
  alert(this.Bstr);
}

function extend(sub,parent){
  var subproto = sub.prototype;
  sub.prototype = parent.prototype;
  typeof subproto != 'object' && (subproto = {});
  typeof sub.prototype != 'object' && (sub.prototype = {});
  for(var i in subproto){
    sub.prototype[i] = subproto[i];
  }
  sub.superclass = parent;
}

// B 繼承 A
extend(B,A);
// C 繼承 B
extend(C,B);

var c = new C(); // 'hello A', 'hello B'
c.funA(); // 'hello A'
c.funB(); // 'hello B'
alert(c instanceof A) // true
alert(c instanceof B) // true

這裡的約定是:每個子類別都有一個 superclass 屬性指向它的父類別;extend 直接把父類別的 prototype 借給子類別使用,不執行父類別建構子。父類別建構子改由子類別自己在建構時呼叫:

arguments.callee.superclass && arguments.callee.superclass.apply(this, arguments);

第三階段:掛到 Function 原型上

Function.prototype.extend = function(parent){
  var subproto = this.prototype;
  this.prototype = parent.prototype;
  typeof subproto != 'object' && (subproto = {});
  typeof this.prototype != 'object' && (this.prototype = {});
  for(var i in subproto){
    this.prototype[i] = subproto[i];
  }
  this.superclass = parent;
  return this;
}

function A(){
  this.Astr = 'hello A';
}
A.prototype = {
  funA:function(){
    alert(this.Astr);
  }
};

var B = function(){
  arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
  this.Bstr = 'hello B';
}
B.prototype = {
  funB:function(){
    alert(this.Astr);
  }
};
B.extend(A);

var C = function(){
  arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
  alert(this.Astr);
  alert(this.Bstr);
}.extend(B);

var c = new C(); // 'hello A', 'hello B'
c.funA(); // 'hello A'
c.funB(); // 'hello B'
alert(c instanceof A) // true
alert(c instanceof B) // true

extend 做的事依序是:1) 用 subproto 保留子類別原本的 prototype;2) 把子類別 prototype 指向父類別的 prototype 物件,讓 instanceof 成立;3) 遍歷 subproto,把原本的子類別成員補回去,同名者自然覆蓋父類別;4) 把 superclass 指向父類別。

關鍵洞察

JavaScript 繼承的本質就是維持原型鏈的連續性——instanceof 判斷的其實是「實例的 __proto__ 鏈上,是否存在與父類別 prototype 同一個物件」。

不過這套寫法有幾個今天必須明說的問題:

  • arguments.callee 已在嚴格模式下被禁用,而且它讓函式無法被最佳化,效能有實質損失。改用具名函式表達式即可取得同樣效果。
  • 共用 prototype 物件意味著 B 往 prototype 上加任何東西,A 也會跟著長出來,汙染是雙向的。標準做法是用一個空的 intermediary 建構子做橋接。
  • 擴充 Function.prototype 會讓所有函式都多出可列舉屬性,這正是第 12968 篇提到要避免的做法。

換成現代寫法,同样的意圖只要幾行:

class A {
  constructor() { this.Astr = 'hello A'; }
  funA() { alert(this.Astr); }
}

class B extends A {
  constructor() {
    super();          // 對應原本的 superclass.apply
    this.Bstr = 'hello B';
  }
  funB() { alert(this.Bstr); }
}

const c = new B();
c.funA();            // 'hello A'
c.funB();            // 'hello B'
c instanceof A;      // true

ES6 的 class 背後仍然是原型鏈,但語法把「建立中繼建構子、修 constructor 指向、呼叫父類別建構子」這些容易出錯的細節都封裝好了。理解上面三個階段的演進過程,你就能明白 class 語法糖到底替你做了什麼。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved