萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 設計模式:類式繼承完整實現與 extend 封裝

類式繼承核心步驟

  1. **子類構造函數中呼叫父類構造函數**(Parent.call(this, ...)),繼承實例屬性。
  2. **子類 prototype 指向父類實例**(Child.prototype = new Parent()),繼承原型方法。
  3. **修正 constructor 指向**(Child.prototype.constructor = Child)。

基礎實現

function Person(name) {
  this.name = name;
}
Person.prototype.getName = function () { return this.name; };

function Blogger(name, blog) {
  Person.call(this, name); // 步驟 1
  this.blog = blog;
}

// 步驟 2 + 3
Blogger.prototype = new Person();
Blogger.prototype.constructor = Blogger;

// 測試
const b = new Blogger('zhenn', 'https://jb51.net');
console.log(b.name);       // 'zhenn' (實例屬性)
console.log(b.blog);       // 'https://jb51.net' (子類屬性)
console.log(b.getName());  // 'zhenn' (繼承原型方法)
console.log(b instanceof Blogger); // true
console.log(b instanceof Person);  // true

缺陷與改良:寄生組合式繼承(最推薦)

上述 new Parent() 會執行父類構造函數,可能產生副作用(如副作用屬性污染原型)。改良:

function inherit(Child, Parent) {
  // 建立中介物件,原型指向 Parent.prototype,不執行 Parent 構造函數
  const F = function () {};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.superclass = Parent.prototype; // 可選:便於呼叫 super
}

// 使用
function Person(name) { this.name = name; }
Person.prototype.getName = function () { return this.name; };

function Blogger(name, blog) {
  Person.call(this, name);
  this.blog = blog;
}
inherit(Blogger, Person);

// 新增子類原型方法
Blogger.prototype.getBlog = function () { return this.blog; };

通用 extend 封裝

function extend(Child, Parent, protoProps = {}, staticProps = {}) {
  // 1. 原型鏈繼承
  const F = function () {};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.superclass = Parent.prototype;

  // 2. 混入原型方法
  Object.assign(Child.prototype, protoProps);

  // 3. 混入靜態屬性/方法
  Object.assign(Child, staticProps);

  return Child;
}

// 使用範例
const Animal = function (name) { this.name = name; };
Animal.prototype.speak = function () { console.log(this.name + ' makes a noise'); };

const Dog = extend(function Dog(name, breed) {
  Animal.call(this, name);
  this.breed = breed;
}, Animal, {
  speak() { console.log(this.name + ' barks'); },
  getBreed() { return this.breed; }
}, {
  DEFAULT_BREED: 'Mixed'
});

const d = new Dog('Buddy', 'Labrador');
d.speak(); // Buddy barks
console.log(Dog.DEFAULT_BREED); // Mixed

ES6 Class extends 實現(語法糖,底層相同)

class Person {
  constructor(name) { this.name = name; }
  getName() { return this.name; }
}

class Blogger extends Person {
  constructor(name, blog) {
    super(name); // 必須先呼叫 super()
    this.blog = blog;
  }
  getBlog() { return this.blog; }
}

const b = new Blogger('zhenn', 'https://jb51.net');
console.log(b.getName()); // zhenn
console.log(b instanceof Person); // true

關鍵差異對照

特性ES5 寄生組合式ES6 Class extends
語法手動組合內建關鍵字
super 呼叫Parent.call(this)super()
靜態繼承需手動 Object.assign自動繼承
原型方法定義Child.prototype.xxx = ...class 體內直接寫
瀏覽器支援IE6+IE10+ (需 transpile)

最佳實踐建議

  • 新專案**優先使用 ES6 Class**,配合 Babel/TypeScript 編譯兼容舊環境。
  • 需支援 IE8- 且無建構工具時,使用 extend 封裝的寄生組合式繼承。
  • 避免多層繼承(>3 層),改用組合/混入模式。
  • 建構函數中**盡量不做副作用操作**(DOM 操作、網路請求),放入 init() 方法由調用者控制。
copyright © 萬盛學電腦網 all rights reserved