I am studying JavaScript's prototype behavior and see a lot of different usages which confuses me. Let's say we have the following constructor functions and prototype definitions.
function Mammal(name) {
this.name = name;
}
Mammal.prototype.says = function ( ) {
return this.saying || '';
};
Mammal.prototype.getName = function ( ) {
return this.name;
};
function Cat(name) {
this.name = name; //another maybe better way could be Mammal.call(this, name); results in the same
this.saying = "meow";
}
Cat.prototype = new Mammal();
Cat.prototype.purr = function() {
return "rrrrrrr";
};
I've seen this kind of examples a lot on the web and in books but it doesn't feel like this is the proper way of doing it. The first problem I have with it, is that the Mammal constructor is called without it's arguments (which are obvious unknown at this point). As a result of that the name property of the new constructed Mammal object is undefined (see image below).
In my opinion this property shouldn't even be there because is has no use and pollutes the object model.
My question: Is the above example which are used a lot to explain some of JavaScript's prototype behavior the accepted way of doing this or is it preferable to do the following:
Cat.prototype = Object.create(Mammal.prototype);
Please release me from this burden. Thanks in advance.