2012-01-31 89 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__