I find the behaviour of this piece of code puzzling, why is the constructor of child not Child? Can someone please explain this to me?
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
As Pointy has pointed out, in his answer
The usual way to deal with this is to augment the object’s prototype
constructorproperty after assigning to theprototypeI can’t recommend Stoyan Stefanov’s Object Oriented JavaScript enough which covers Prototype and Inheritance in some detail (get the second edition if you can as it addresses some critiques of the first edition).