I\'ve created an example to illustrate:
// this is the parent class
function animal() { console.log(\'animal constructor\') }
// allow animals to walk
anima
Think about the line:
cat.prototype = Object.create(animal.prototype);
Object.create(animal.prototype)
will simply return an object that has no properties itself but whose prototype is given by animal.prototype
. Therefore looking up the constructor
property on this object will simply return the value held in animal.prototype.constructor
.
Since the above line is assigning a reference to this newly created object to cat.prototype
(and hence overwriting whatever was previously held in cat.prototype
), of course you'd expect cat.prototype.constructor
to be equal to animal.prototype.constructor
.
As you mentioned above, you can side-step this 'issue' using something like:
cat.prototype.constructor = cat;
reference for Object.create(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create