2015-06-16 73 views
0

爲什麼調用b.__proto__返回Object{}而不是a爲什麼調用實例__proto__返回Object {}而不是它設置的實例?

var a = { 
    x: 10, 
    calculate: function (z) { 
    return this.x + this.y + z; 
    } 
}; 

var b = { 
    y: 20, 
    __proto__: a 
}; 

var c = { 
    y: 30, 
    __proto__: a 
}; 

enter image description here

+0

你是什麼意思,我測試和'b .__ proto__ ===了' – Hacketo

+0

@Hacketo見附件快照 – Blake

+1

你期待的是,當你在控制檯輸入'了'返回'了'? 'Object {x:10}'是'a'的值 – Hacketo

回答

0

爲什麼callling灣proto返回Object {}而不是?

因爲名稱a只是一個名稱,指示如何用x = 10和計算方法引用該對象。

例如 - 如果您returnc從該代碼(作爲函數)名稱a將是完全沒有意義的。

function foo(){ 
    var y = {x: 5}; 
    var z = { __proto__ : y }; // better to use Object.create 
    return `; 
} 

// in someone else's code 
var a = foo(); 
a.__proto__; // y wouldn't mean anything to me here, I'm not even aware of that it 
a.x; // 5 
相關問題