JS 沒有類別,只有**物件**。每個物件內部有 [[Prototype]] (對外暴露為 __proto__) 指向另一個物件。尋找屬性時沿著這條鏈向上查找,直到 null。
const obj = { a: 1 };
Object.getPrototypeOf(obj) === Object.prototype; // true
Object.prototype.__proto__ === null; // 鏈尾
function Animal(name) {
this.name = name;
}
Animal.prototype.say = function() { console.log(this.name); };
const dog = new Animal("WangWang");
// new 運算子做三件事:
// 1. 建立新物件:const obj = {}
// 2. 連結原型:obj.__proto__ = Animal.prototype
// 3. 綁定 this:Animal.call(obj, "WangWang")
// 4. 回傳 obj (若建構函式無手動 return 物件)
dog.say(); // "WangWang" — 找不到 say,沿 __proto__ 找到 Animal.prototype.say
function Animal(name) { this.name = name; }
Animal.prototype.say = function() { console.log(this.name); };
function Dog(name, breed) {
Animal.call(this, name); // 繼承實例屬性 (呼叫 Parent 建構函式)
this.breed = breed;
}
// 關鍵:建立原型鏈
Dog.prototype = Object.create(Animal.prototype); // 推薦:不執行 Animal 建構函式
Dog.prototype.constructor = Dog; // 修正 constructor 指向
Dog.prototype.bark = function() { console.log("Woof!"); };
const d = new Dog("Buddy", "Golden");
d.say(); // "Buddy" (來自 Animal.prototype)
d.bark(); // "Woof!" (來自 Dog.prototype)
console.log(d instanceof Dog, d instanceof Animal); // true true
上述即為組合繼承:Animal.call(this) 繼承實例屬性 + Object.create 繼承原型方法。**最常用、最穩健**。
class Animal {
constructor(name) { this.name = name; }
say() { console.log(this.name); }
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // 必須先呼叫 super(),才能使用 this
this.breed = breed;
}
bark() { console.log("Woof!"); }
}
// 編譯後本質等同組合繼承模式
console.log(Dog.prototype.__proto__ === Animal.prototype); // true
console.log(Dog.__proto__ === Animal); // true (靜態方法也繼承)
function F() {}
const f = new F();
// 關係圖:
// f.__proto__ === F.prototype
// F.prototype.__proto__ === Object.prototype
// F.__proto__ === Function.prototype
// Function.prototype.__proto__ === Object.prototype
// Object.prototype.__proto__ === null




| 策略 | 原理 | 優點 | 缺點 |
|---|---|---|---|
| 構造複製 | 每 new 一次完整複製原型屬性 | 物件獨立,讀寫互不影響 | 記憶體浪費極大,O(N) 空間 |
| 寫時複製 (COW) | 讀時共享原型,寫時才複製該屬性到實例 | 讀取快、記憶體省 | 首次寫入有複製開銷;頻繁寫入退化為構造複製 |
| 讀遍歷 (JS 採用) | 實例只存自身屬性,讀取時沿 __proto__ 向上查找 | 記憶體最省、建立實例最快、動態性強 (原型改變即時生效) | 深層原型鏈查找有微小效能成本 (現代引擎內聯快取優化極佳) |
Dog.prototype = new Animal(); // 繼承
console.log(Dog.prototype.constructor === Animal); // true ❌ 指向父類
Dog.prototype.constructor = Dog; // 必須手動修正
console.log(Dog.prototype.constructor === Dog); // true ✅
使用 Object.create(Animal.prototype) 同樣需手動修正 constructor。
function A() {}
A.prototype.x = 1;
const a = new A();
console.log(a.x); // 1
A.prototype.x = 2; // 修改原型
console.log(a.x); // 2 ✅ 即時生效 (讀遍歷特性)
a.x = 3; // 在實例建立自有屬性 (遮蔽)
console.log(a.x); // 3
delete a.x;
console.log(a.x); // 2 (恢復原型值)
const dict = Object.create(null); // __proto__ = null
dict["toString"] = "custom"; // 不會衝突 Object.prototype.toString
// 適合做 Map/字典,避免原型污染攻擊
class extends** — 語法清晰、錯誤少、靜態分析友善。Object.assign(Child.prototype, mixin) 或 Object.setPrototypeOf (慎用,影響優化)。constructor** — 除非刻意不修正。prototype,實例資料放 this。__proto__ (非標準但通用) vs Object.getPrototypeOf/SetPrototypeOf (標準 API)。
實例 (dog)
│ __proto__
▼
Dog.prototype { bark(), constructor: Dog }
│ __proto__
▼
Animal.prototype { say(), constructor: Animal }
│ __proto__
▼
Object.prototype { toString(), hasOwnProperty... }
│ __proto__
▼
null
掌握原型鏈機制,才能真正理解 JS 物件導向精髓,無論是除錯 instanceof 異常、設計繼承體系、還是優化效能,都是核心基石。