DIV CSS 佈局教程網

__proto__ 與 prototype:JavaScript 原型機制全解析
編輯:關於JavaScript     

這兩個名字只差幾個字,含義卻完全不同。__proto__ 是內部原型,prototype 是建構函數原型(而建構函數本質上就是函數)。搞懂這條線,JavaScript 的物件模型就不再神秘。

一、所有建構函數都繼承自 Function.prototype

內建的 Number、Boolean、String、Object、Function、Array、RegExp、Error、Date,它們的 __proto__ 全部指向 Function.prototype:

Number.__proto__  === Function.prototype   // true
Boolean.__proto__ === Function.prototype   // true
String.__proto__  === Function.prototype   // true
Object.__proto__  === Function.prototype   // true
Function.__proto__ === Function.prototype  // true
Array.__proto__   === Function.prototype   // true
RegExp.__proto__  === Function.prototype   // true
Error.__proto__   === Function.prototype   // true
Date.__proto__    === Function.prototype   // true

這說明它們都是建構函數,本質上是 Function 的實例,等價於:

var Number = new Function();

JavaScript 的內建建構函數/物件共 12 個(ES5 新增 JSON),上面列了可直接存取的 9 個。其餘如 Global 無法直接存取、Arguments 只在函數呼叫時由引擎建立,而 Math 與 JSON 是以物件形式存在、無需 new:

Math.__proto__ === Object.prototype   // true
JSON.__proto__ === Object.prototype   // true

自訂的函數也不例外,無論函數聲明或函數表達式:

function Person() {}
var Man = function () {};

console.log(Person.__proto__ === Function.prototype); // true
console.log(Man.__proto__    === Function.prototype); // true

結論:所有建構函數都來自 Function.prototype,包含根建構函數 Object 與 Function 自身。因此它們都繼承了 length、call、apply、bind 這些方法。

二、Function.prototype 是唯一 typeof 為 function 的 prototype

console.log(typeof Function.prototype); // "function"
console.log(typeof Object.prototype);   // "object"
console.log(typeof Number.prototype);   // "object"
console.log(typeof Boolean.prototype);  // "object"
console.log(typeof String.prototype);   // "object"
console.log(typeof Array.prototype);    // "object"
console.log(typeof RegExp.prototype);   // "object"
console.log(typeof Error.prototype);    // "object"
console.log(typeof Date.prototype);     // "object"

其他建構函數的 prototype 都是一個物件。而 Function.prototype 本身是一個空函數,在控制台直接印出來就能看見。

三、Function.prototype 的原型是 Object.prototype

既然所有建構函數的 __proto__ 是 Function.prototype,那它的 __proto__ 又是誰?

console.log(Function.prototype.__proto__ === Object.prototype); // true

這正體現了「函數也是一等公民」:所有建構函數同時也是普通的 JS 物件,可以隨意增刪屬性,並繼承 Object.prototype 上的 toString、valueOf、hasOwnProperty 等方法。

四、原型鏈的終點是 null

Object.prototype.__proto__ === null; // true

到頂了。原型鏈到此結束,這也是為什麼 obj.toString() 永遠不會因為找不到而報錯——它一定能在鏈的某處被找到,除非你把它設成 null。

五、把整條鏈串起來看

// 實例 → 建構函數.prototype → Object.prototype → null
var arr = [];
arr.__proto__            === Array.prototype   // true
Array.prototype.__proto__ === Object.prototype // true
Object.prototype.__proto__ === null            // true

// 建構函數 → Function.prototype → Object.prototype → null
Array.__proto__             === Function.prototype // true
Function.prototype.__proto__ === Object.prototype  // true

兩條鏈在 Object.prototype 交會,這就是整個 JavaScript 物件系統的骨架。

實務提醒

__proto__ 是各家瀏覽器早期實作的非標準屬性,標準版本是 Object.getPrototypeOf() 與 Object.setPrototypeOf()。前者請優先使用;後者因為會破壞引擎的內部優化,效能不佳,除了原型切換這類特殊需求外應避免。ES6 的 class 語法背後跑的就是這套原型鏈,理解它之後,super 的行為也會變得很自然。

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