**原型是一個對象,其他對象通過它繼承屬性。** 每個對象內部有一個隱藏槽位 `[[Prototype]]` (ES6 標準化為 `Object.getPrototypeOf`),指向其原型對象。形成鏈式結構,頂端是 `Object.prototype` (其 `[[Prototype]]` 為 `null`)。
const obj = {};
// 1. 標準方法 (ES5+)
Object.getPrototypeOf(obj); // {constructor: ƒ, __defineGetter__: ƒ, ...}
// 2. 非標準但廣泛支持 (除 IE 外)
obj.__proto__; // 同 getPrototypeOf
// 3. 構造函數 prototype 屬性 (間接、易混淆)
obj.constructor.prototype; // 同樣指向 {constructor: ƒ, ...}
// 關鍵區別:
// obj.__proto__ === obj.constructor.prototype // true (通常)
// 但修改構造函數.prototype 不影響已創建實例的 __proto__
const a = {x: 1};
const b = Object.create(a); // b.[[Prototype]] = a
const c = Object.create(b); // c.[[Prototype]] = b
c.x; // 1 (c → b → a 找到)
c.y = 2; // 在 c 自身創建 y,不影響原型鏈
c.x = 3; // 在 c 自身創建 x (屬性遮蔽)
delete c.x; // 刪除自身屬性,恢復繼承自 a 的 x
function Circle(radius) {
this.radius = radius;
// 隱式:this.__proto__ = Circle.prototype;
}
// Circle.prototype 是什麼?
// 1. 它是一個普通對象 {constructor: Circle}
// 2. 所有 new Circle() 實例的 [[Prototype]] 都指向它
// 3. 在其上定義方法,實例共享
Circle.prototype.area = function() {
return Math.PI * this.radius ** 2;
};
const c1 = new Circle(3);
const c2 = new Circle(4);
c1.area(); // 28.27
c2.area(); // 50.27
// 關鍵:c1.area === c2.area === Circle.prototype.area (同一函數引用)
function A(name) { this.name = name; }
const a = new A('alpha');
// 情況 1:修改現有 prototype 對象的屬性 → 實例可見
A.prototype.x = 23;
a.x; // 23 ✅ (a.__proto__ 仍指向同一對象)
// 情況 2:替換 prototype 為新對象 → 實例不可見
A.prototype = {y: 42};
a.y; // undefined ❌ (a.__proto__ 仍指向舊對象)
new A('beta').y; // 42 ✅ (新實例指向新 prototype)
function F() {}
F.prototype;
// {constructor: ƒ F, __proto__: Object.prototype}
// 注意:F.prototype.constructor === F
function Parent() { this.colors = ['red']; }
function Child() {}
Child.prototype = new Parent(); // 實例作為原型
const c1 = new Child();
c1.colors.push('blue');
const c2 = new Child();
c2.colors; // ['red', 'blue'] ❌ 引用類型共享污染
function Child(name) {
Parent.call(this); // 每個實例獨立 colors
this.name = name;
}
// 但方法無法復用 (每個實例創建新函數)
function Child(name) {
Parent.call(this); // 實例屬性
this.name = name;
}
Child.prototype = Object.create(Parent.prototype); // 原型鏈
Child.prototype.constructor = Child; // 修正 constructor
function inherit(Child, Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.super = Parent.prototype; // 便於 super 調用
}
class Parent {
constructor() { this.colors = ['red']; }
getColors() { return this.colors; }
}
class Child extends Parent {
constructor(name) {
super(); // 必須先調用 super()
this.name = name;
}
}
// 編譯後本質:
// function Child(name) { Parent.call(this); this.name = name; }
// Child.prototype = Object.create(Parent.prototype);
// Child.prototype.constructor = Child;
// Object.setPrototypeOf(Child, Parent); // 靜態方法繼承
// 創建新對象,指定其 [[Prototype]]
const proto = {greet() { console.log('hi'); }};
const obj = Object.create(proto);
obj.greet(); // hi
obj.__proto__ === proto; // true
// 等價於 (舊)
const obj2 = {};
Object.setPrototypeOf(obj2, proto); // ES6 標準化
// 或
const obj3 = {__proto__: proto}; // 對象字面量語法 (ES6)
| 代碼 | 結果 | 解析 |
|---|---|---|
| `Function.prototype.bind` | Function | 函數的原型是 Function.prototype |
| `Object.prototype.__proto__` | null | 原型鏈頂端 |
| `[] instanceof Array` | true | Array.prototype 在鏈上 |
| `Object.create(null)` | {} | 無原型對象 (純字典),無 constructor/toString |
| `__proto__` vs `prototype` | 實例 vs 構造函數 | 實例有 `__proto__`,構造函數有 `prototype` |
JavaScript 的繼承本質是**基於原型的委托**。理解 `[[Prototype]]` 內部槽位、構造函數 `prototype` 屬性與實例 `__proto__` 的區別、屬性查找遮蔽規則、以及 `Object.create`/`class extends` 的底層機制,是掌握 JS 面向對象的核心。現代開發優先使用 `class` 語法糖,但必須理解其原型本質。