0
function inheritPrototype(childObject, parentObject) {
    var copyOfParent = Object.create(parentObject.prototype);
    copyOfParent.constructor = childObject;
    childObject.prototype = copyOfParent;
}

Why is

copyOfParent.constructor = childObject;

needed?

A source on the internet explained:

Then we set the constructor of this new object to point to the childObject. This step is necessary because the preceding step overwrote the childObject constructor when it overwrote the childObject prototype (during the Object.create() process)

How can it have overwritten the childObject constructor when this is the first time the childObject parameter is being used?

Tomatoes
  • 93
  • 1
  • 7
  • It did "change" the `childObject.prototype.constructor` property by overwriting the whole `childObject.prototype` with a different object (whose `constructor` property differs from `childObject`). – Bergi Nov 26 '13 at 00:23
  • @Bergi I understand the constructor property but in that case why isnt it changing Constructor to the constructor of childObject instead. childObject has a different constructor than itself surely. normally we do constructor: User . Need to have a reference to the actual constructor function that created it – Tomatoes Nov 26 '13 at 00:33
  • 1
    You do understand that both `childObject` and `parentObject` are constructor functions in here? They're admittedly oddly named. The function does not create a prototype chain between them, but does set up prototypical inheritance for their instances. – Bergi Nov 26 '13 at 00:36
  • pls post an answer and i will pick – Tomatoes Nov 26 '13 at 00:45

0 Answers0