2012-01-31 88 views
2

原型之間的關係我寫的代碼要弄清楚我__proto__實例及其在JavaScript構造的原型之間的關係:__proto__實例和其構造的在JavaScript

// Constructor 
var Guy = function(name) { 
     this.name = name; 
}; 

// Prototype 
var chinese = { 
     region: "china", 
     myNameIs: function() { 
      return this.name; 
     } 
}; 

Guy.prototype = chinese; 

var he = new Guy("Wang"); 
var me = new Guy("Do"); 

我得到了false我測試.__ proto__我是否等於中國:

console.log("__proto__ of me is chinese? " + chinese == me.__proto__); // logs false 

他們爲什麼不一樣的東西?

回答

3

因爲+==更高的優先級,所以你真的這樣做......

("__proto__ of me is chinese? " + chinese) == me.__proto__ 

你需要做的是...

"__proto__ of me is chinese? " + (chinese == me.__proto__) 

,或者使用在一個,console呼叫通過單獨的參數...

"__proto__ of me is chinese? ", chinese == me.__proto__