DIV CSS 佈局教程網

__proto__ 與 prototype 的關係:JavaScript 原型鏈全解析
編輯:JavaScript基礎知識     

談原型,繞不開兩個長得很像但意義完全不同的東西:物件的內部原型 __proto__,以及構造器的原型屬性 prototype。下面分兩條主線把它們的關係理清。

一、所有構造器/函數的 __proto__ 都指向 Function.prototype

而 Function.prototype 本身是一個空函數(Empty function)。

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

JavaScript 的內建構造器/物件共有 12 個(ES5 新增了 JSON),上面列出了可直接存取的那幾個。剩下的如 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)。

Function.prototype 也是唯一一個 typeof XXX.prototype 為 "function" 的 prototype,其他構造器的 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

既然它是個「空函數」,不妨 alert(Function.prototype) 親眼確認一下。

那 Function.prototype 的 __proto__ 又是誰?這裡正好能體現「函數是一等公民」這句話:

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

也就是說,所有構造器同時也是普通的 JS 物件,可以隨意增刪屬性,並繼承了 Object.prototype 上的 toString、valueOf、hasOwnProperty 等方法。

最後,Object.prototype 的 __proto__ 是誰?

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

到頂了,是 null,原型鏈到此終結。

二、所有物件的 __proto__ 都指向其構造器的 prototype

先看引擎內建構造器產生的實例:

var obj = {name: 'jack'}
var arr = [1,2,3]
var reg = /hello/g
var date = new Date
var err = new Error('exception')
console.log(obj.__proto__ === Object.prototype)  // true
console.log(arr.__proto__ === Array.prototype)   // true
console.log(reg.__proto__ === RegExp.prototype)  // true
console.log(date.__proto__ === Date.prototype)   // true
console.log(err.__proto__ === Error.prototype)   // true

再看自定義構造器:

function Person(name) {
  this.name = name
}
var p = new Person('jack')
console.log(p.__proto__ === Person.prototype) // true

p 是 Person 的實例,它的內部原型恆指向構造器 Person 的 prototype。由於每個物件都有 constructor 屬性可以取得它的構造器,下面這行同樣恆成立:

console.log(p.__proto__ === p.constructor.prototype) // true

修改原型 vs 重寫原型

前面的 Person 沒有為原型添加任何成員。若改成「修改」原型:

function Person(name) {
  this.name = name
}
// 修改原型
Person.prototype.getName = function() {}
var p = new Person('jack')
console.log(p.__proto__ === p.constructor.prototype) // true

但如果換成「重寫」原型,結果就不同了:

function Person(name) {
  this.name = name
}
// 重寫原型
Person.prototype = {
  getName: function() {}
}
var p = new Person('jack')
console.log(p.__proto__ === Person.prototype)         // true
console.log(p.__proto__ === p.constructor.prototype)  // false

原因不難理解:賦給 Person.prototype 的是一個物件直接量 {getName: function(){}},而直接量物件的 constructor 指向根構造器 Object,Object.prototype 是一個空物件,自然不等於帶有 getName 的那個物件。

var p = {}
console.log(Object.prototype)                   // 一個空物件 {}
console.log(p.constructor === Object)           // 直接量物件的 constructor 為 Object
console.log(p.constructor.prototype === Object.prototype) // true

實務上的結論是:想替換原型時,記得把 constructor 補回去,否則 instanceof 之外的型別判斷邏輯可能會出錯。

兼容性備註

__proto__ 在 IE6/7/8/9 中都不支援。IE9 起可以使用 ES5 的 Object.getPrototypeOf() 來取得物件的內部原型:

var p = {}
var __proto__ = Object.getPrototypeOf(p)
console.log(__proto__ === Object.prototype) // true

而 __proto__ 本身只是各家引擎的非標準擴充,正式規範中的對應能力是 Object.getPrototypeOf() 與 Object.setPrototypeOf()。

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