Why is it important that I reset the constructor from Mammal to Cat? I played with this code and did not find any negative consequences of having the "wrong" constructor.
function Mammal(name){ this.name=name; this.offspring=[]; } Cat.prototype = new Mammal(); // Here where the inheritance occurs Cat.prototype.constructor=Cat; // Otherwise instances of Cat would have a constructor of Mammal function Cat(name){ this.name=name; }
eg:
function Mammal(name){ this.name=name; this.offspring=[]; } Cat.prototype = new Mammal(); // Here where the inheritance occurs function Cat(name){ this.name=name; this.hasFur = true; } c1 = new Cat(); alert(c1.hasFur); //returns true;
Technically, you don’t need to do this, but since you are replacing the whole object .prototype, you lose .constructorthat which is placed there by default, so if you have code that relies on it, you need to manually enable it.
.prototype
.constructor