為了共用一些方法,JS 把 toString 和 valueOf 放在一個對象里(暫且叫做公用屬性組成的對象)
然后讓每一個對象的 __proto__ 存儲這個「公用屬性組成的對象」的地址。
__proto__的讀取器(getter)暴露了一個對象的內(nèi)部 [[Prototype]] 。對于使用對象字面量創(chuàng)建的對象,這個值是 Object.prototype。對于使用數(shù)組字面量創(chuàng)建的對象,這個值是 Array.prototype。對于functions,這個值是Function.prototype。對于使用 new fun 創(chuàng)建的對象,其中fun是由js提供的內(nèi)建構(gòu)造器函數(shù)之一(Array, Boolean, Date, Number, Object, String 等等),這個值總是fun.prototype。對于用js定義的其他js構(gòu)造器函數(shù)創(chuàng)建的對象,這個值就是該構(gòu)造器函數(shù)的prototype屬性
可以打得出以下結(jié)論
var 對象 = new 函數(shù)()
對象.__proto__=== 對象的構(gòu)造函數(shù).prototype
又有以下推論
var number = new Number()
number.__proto__ = Number.prototype
Number.__proto__ = Function.prototype // 因為 Number 是 Function 的實例
var object = new Object()
object.__proto__ = Object.prototype
Object.__proto__ = Function.prototype // 因為 Object 是 Function 的實例
var function = new Function()
function.__proto__ = Function.prototype
Function.__proto__ == Function.prototye // 因為 Function 是 Function 的實例