prototype 與 __proto__ 只差兩個底線,卻是很多人對原型鏈最困惑的地方。先把定義分清楚:
一句話記法:__proto__ 是「內部原型」,prototype 是「建構器原型」(建構器就是函數)。
要理解它們的關係,先回答一個問題:什麼是建構器?想建立一個物件,得先有物件建構器,就像 PHP 裡要先有類。而建構器的實體就是一個函數,透過 new 就能用它建立物件。
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
這說明 Number 等都是建構器,而它們其實都是 Function 的實例,等同於 var Number = new Function();。
JavaScript 內建的建構器/物件共有 12 個(ES5 新增 JSON),上面列了可直接存取的 8 個。剩下的如 Global 無法直接存取,Arguments 只在函數呼叫時由引擎建立,Math 與 JSON 則是以物件形式存在、無需 new——它們的 __proto__ 是 Object.prototype:
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 自身。因此所有建構器都繼承了 Function.prototype 的 length、call、apply、bind(ES5)等成員。
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 還是個空函數,alert(Function.prototype) 就會看到它的真面目。
console.log(Function.prototype.__proto__ === Object.prototype); // true
這正好體現了「函數是一等公民」:所有建構器同時也是普通的 JavaScript 物件,可以幫它們增刪屬性,並且繼承了 Object.prototype 上的 toString、valueOf、hasOwnProperty 等方法。
Object.prototype.__proto__ === null; // true
把四條規則串起來,就是那條所有人都在找答案的原型鏈:
obj --__proto__--> Person.prototype Person --__proto__--> Function.prototype Function.prototype --__proto__--> Object.prototype Object.prototype --__proto__--> null
而 new Person() 生出來的物件,其 __proto__ 指向的是 Person.prototype(也就是函數身上那個 prototype 屬性),這才是兩者真正的接合點:函數的 prototype 屬性,會成為該函數實例的 __proto__。

最後補充:ES6 起標準做法是使用 Object.getPrototypeOf(obj) 與 Object.setPrototypeOf(obj, proto),非標準的 obj.__proto__ 僅建議用於除錯與教學。