In the following code , what's the benefit of having a constructor for Cat?
function Mammal(){
}
Mammal.prototype.breathe = function(){
// do some breathing
};
function Cat(){
}
Cat.prototype = new Mammal();
Cat.prototype.constructor = Cat;
var garfield = new Cat();
console.log(garfield instanceof Cat);
With or without the constructor, it always prints true
, as the result of checking instanceof
.
In general do we have to bother setting the constructor?