I\'ve been trying to wrap my head around the new Object.create
method which was introduced in ECMAScript 5.
Usually when I want to use inheritance I do
First, running the Animal
constructor may have undesired side effects. Consider this:
var Animal = function(name) {
this.name = name;
Animal.instances.push(this);
};
Animal.instances = [];
This version would keep track of all instances that have been created. You don't want your Dog.prototype
to be recorded there.
Second, Dog.prototype = Animal.prototype
is a bad idea, since that would mean that bark
would become a method of Animal
.