有人可以解释为什么会这样吗
function Human(name) {
this.name = name;
}
var george = new Human('George');
alert(george.constructor === Human)
这表明是正确的。而
var monkey = {
hair: true,
feeds: 'bananas',
breathes: 'air'
};
function Human(name) {
this.name = name;
}
Human.prototype = monkey;
var george = new Human('George');
alert(george.constructor === Human)
这显示错误
答案 0 :(得分:2)
constructor
继承自prototype
。由于您要将Human
的{{1}}更改为prototype
monkey
,Object
现在将返回george.constructor()
而不是Object {}
{1}}。
值得注意的是Human {}
将保持不变:
instanceof
var george = new Human('George');
george.constructor === Human; // true
george instanceof Human; // true