我觉得这段代码的行为令人费解,为什么child
的构造函数不是Child
?有人可以向我解释这个吗?javascript对象模型:奇怪的构造函数属性
function Parent() {
this.prop1 = "value1";
this.prop2 = "value2";
}
function Child() {
this.prop1 = "value1b";
this.prop3 = "value3";
}
Child.prototype = new Parent();
var child = new Child();
// this is expected
console.log(child instanceof Child); //true
console.log(child instanceof Parent); //true
// what??
console.log(child.constructor == Child); //false
console.log(child.constructor == Parent); //true
这个问题似乎是[this one]的一个副本(http://stackoverflow.com/questions/2479349/constructors-and-inheritance-in-js) - 但我觉得这个问题的答案更有用。 + 1s全部:) – 2010-10-29 02:22:04