分配原型后无法访问构造函数

时间:2014-10-14 11:12:53

标签: javascript

有人可以解释为什么会这样吗

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)

这显示错误

1 个答案:

答案 0 :(得分:2)

constructor继承自prototype。由于您要将Human的{​​{1}}更改为prototype monkeyObject现在将返回george.constructor()而不是Object {} {1}}。

值得注意的是Human {}将保持不变:

instanceof
var george = new Human('George');
george.constructor === Human;        // true
george instanceof Human;             // true
相关问题