Why is it important that I reset the constructor from Mammal to Cat? I’ve been playing around with this code and haven’t found any negative effects of having the “wrong” constructor.
function Mammal(name){
this.name=name;
this.offspring=[];
}
Cat.prototype = new Mammal(); // Here's where the inheritance occurs
Cat.prototype.constructor=Cat; // Otherwise instances of Cat would have a constructor of Mammal
function Cat(name){
this.name=name;
}
for example:
function Mammal(name){
this.name=name;
this.offspring=[];
}
Cat.prototype = new Mammal(); // Here's 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 that, but since you’re replacing the entire
.prototypeobject, you’re losing the.constructorthat’s put there by default, so if you have code that relies on it, you need to manually include it.