There is two constructor functions: Animal and Rabbit (classes emulation). How can I set inheritance for this classes using prototypes properly? I seen 2 versions:
Rabbit.prototype.__proto__ = Animal.prototype;
I think it works good as I expected. But this version doesn't work in IE10-, and there is the following version:
Rabbit.prototype = Object.create(Animal.prototype);
This version looks good too, but I must create an additional object for it. Which version is preferred? May be there is anything else? Thanks in advance.
Rabbit.prototype = Object.create(Animal.prototype); is prefered.
Mozilla Developer Network (MDN) gives the following warning about
You should have something like this:
Rabbit.prototype = Object.create(Animal.prototype); Rabbit.prototype.constructor = Rabbit;
If you won't write second line instance of
Rabbit will have
Also it's possible to do:
Rabbit.prototype = new Animal();
but in this case you will also trigger constructor, which is not preferred.