核心概念圖解
[Function.prototype] ←──┐
↑ │
│ .__proto__ │ .prototype.__proto__
│ ▼
[Function] ──────► [Object.prototype] ──► null
│ ▲
│ .prototype │ .__proto__
▼ │
[Object] ──────────────┘
關鍵結論
- **所有函數**都是 `Function` 的實例:`Function instanceof Function === true`。
- **所有對象**最終繼承自 `Object.prototype`:`Object.prototype.__proto__ === null`。
- `Function.prototype` 是**函數的原型**,也是**函數**;`Object.prototype` 是**對象的原型**,是**對象**。
- `Function.__proto__ === Function.prototype`(函數的構造函數是 Function 自身)。
- `Object.__proto__ === Function.prototype`(Object 構造函數是函數)。
- `Function.prototype.__proto__ === Object.prototype`(函數原型繼承自對象原型)。
驗證代碼(可在控制台直接運行)
// 1. 基礎關係
console.log("Function instanceof Function:", Function instanceof Function); // true
console.log("Function instanceof Object:", Function instanceof Object); // true
console.log("Object instanceof Function:", Object instanceof Function); // true
console.log("Object instanceof Object:", Object instanceof Object); // true
// 2. __proto__ 鏈
console.log("Function.__proto__ === Function.prototype:", Function.__proto__ === Function.prototype); // true
console.log("Function.prototype.__proto__ === Object.prototype:", Function.prototype.__proto__ === Object.prototype); // true
console.log("Object.__proto__ === Function.prototype:", Object.__proto__ === Function.prototype); // true
console.log("Object.prototype.__proto__:", Object.prototype.__proto__); // null
// 3. 普通函數與構造函數
function A() {}
var a = new A();
console.log("a.__proto__ === A.prototype:", a.__proto__ === A.prototype); // true
console.log("A.__proto__ === Function.prototype:", A.__proto__ === Function.prototype); // true
console.log("A.prototype.__proto__ === Object.prototype:", A.prototype.__proto__ === Object.prototype); // true
// 4. 原型鏈查找演示
Function.prototype.sayHi = function() { console.log("Hi from Function.prototype"); };
Object.prototype.sayHello = function() { console.log("Hello from Object.prototype"); };
A.sayHi(); // "Hi from Function.prototype" (A 是函數,沿 Function.prototype 找到)
a.sayHello(); // "Hello from Object.prototype" (a 是對象,沿 A.prototype → Object.prototype 找到)
A.sayHello(); // "Hello from Object.prototype" (A 也是對象,同理)
Object.sayHi(); // "Hi from Function.prototype" (Object 是函數)
繼承實戰:原型鏈繼承 vs 組合繼承 vs 寄生組合繼承
1. 原型鏈繼承(純原型)
function Parent(name) { this.name = name; this.colors = ['red']; }
Parent.prototype.sayName = function() { console.log(this.name); };
function Child() {}
Child.prototype = new Parent(); // 關鍵:Child.prototype 是 Parent 實例
Child.prototype.constructor = Child;
var child1 = new Child();
child1.colors.push('blue');
var child2 = new Child();
console.log(child2.colors); // ['red', 'blue'] ← 引用類型共享問題!
2. 構造函數借用(解決引用共享)
function Child(name) {
Parent.call(this, name); // 在 Child 實例上執行 Parent 構造函數
}
Child.prototype = new Parent(); // 仍需原型鏈繼承方法
Child.prototype.constructor = Child;
// 缺點:Parent 構造函數執行兩次(一次在 Child.prototype = new Parent(),一次在 Child.call)
3. 組合繼承(最常用經典模式)
function Child(name, age) {
Parent.call(this, name); // 第二次調用:實例屬性
this.age = age;
}
Child.prototype = new Parent(); // 第一次調用:建立原型鏈
Child.prototype.constructor = Child;
Child.prototype.sayAge = function() { console.log(this.age); };
4. 寄生組合繼承(完美模式,ES5 標準寫法)
function inheritPrototype(Child, Parent) {
var prototype = Object.create(Parent.prototype); // 創建中間對象,原型指向 Parent.prototype
prototype.constructor = Child; // 修正 constructor
Child.prototype = prototype; // 賦值
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
inheritPrototype(Child, Parent); // 只調用一次 Parent 構造函數
Child.prototype.sayAge = function() { console.log(this.age); };
5. ES6 Class 语法糖(底層仍是寄生組合繼承)
class Parent {
constructor(name) { this.name = name; this.colors = ['red']; }
sayName() { console.log(this.name); }
}
class Child extends Parent {
constructor(name, age) {
super(name); // 必須先調用 super()
this.age = age;
}
sayAge() { console.log(this.age); }
}
原型鏈屬性查找規則總結
- 對象自有屬性(`hasOwnProperty` 為 true)。
- 對象 `__proto__`(即構造函數 `prototype`)上的屬性。
- 順著 `__proto__` 繼續向上,直到 `null`。
- **實例不會查找自身的 `prototype`**(只有構造函數有 `prototype`,實例只有 `__proto__`)。
常見面試題
- `Function.prototype.bind` 返回的函數 `length` 為何? → 參數個數減去綁定的參數數。
- `Object.create(null)` 創建的對象無原型,`__proto__` 為 `undefined`,可作純字典。
- 箭頭函數無 `prototype`,無 `arguments`,無 `this`,不可作構造函數。