So I was reading the book Head First Javascript Programming, which is a great book btw, learned me a lot, when I stumbled into the subject of multiple inheritance.
The book taught me that to achieve multiple inheritance you should set the prototype of your constructor to a new object of your parent.
As an example:
//Parent
function Dog (name){
this.name = name;
};
//Child
function AwesomeDog(name){
Dog.call(this, name);
};
AwesomeDog.prototype = new Dog();
AwesomeDog.prototype.constructor = AwesomeDog;
AwesomeDog.prototype.isAwesome = true;
Now I don't agree that this is the best way to do it, you call the Dog constructor only for its prototype property, but you also get the name property with it, that you will typically never use and therefore always be undefined.
Why not just do this:
AwesomeDog.prototype = Dog.prototype;
You're only interested in the prototype anyway so why not?
In my opinion this is better because of 2 reasons:
- The overhead of creating a new object is left out (insignificant, but it's something)
- You don't have useless properties floating around in your child's prototype, that will probably always remain undefined.
Now this book has been brilliant in every way so that led me to believe that I perhaps missed something crucial, but I can not see what. I can not imagine any use cases where having double properties (one in the child object and the undefined version in your prototype) can be useful.
Is my way of thinking correct and can I stick with my method?