In the following I assume you are only interested in why Object.create
is preferable for setting up inheritance.
To understand the benefits, lets first clarify what a "class" is made of in JavaScript. You have two parts:
The constructor function. This function contains all the logic to create an instance of the "class", i.e. instance specific code.
The prototype object. This is the object the instance inherits from. It contains all methods (and other properties) that should be shared among all instances.
Inheritance establishes an is-a relation, for example, a Dog
is an Animal
. How is this expressed in terms of constructor function and prototype object?
Obviously a dog must have the same methods as an animal, that is the Dog
prototype object must somehow incorporate the methods from the Animal
prototype object. There are multiple ways to do this. You will often see this:
Dog.prototype = new Animal();
This works because an Animal
instance inherits from the Animal
prototype object. But it also implies that every dog inherits from one specific Animal
instance. That seems to be a bit strange. Shouldn't instance specific code only be run in the constructor function? Suddenly instance specific code and prototype methods seem to be mixed.
We don't actually want to run Animal
instance specific code at that moment, we only want all the methods from the Animal
prototype object. That is what Object.create
lets us do:
Dog.prototype = Object.create(Animal.prototype);
Here we are not creating a new Animal
instance, we only get the prototype methods. The instance specific code is executed exactly where it should be, inside the constructor:
function Dog() {
Animal.call(this, 'Dog');
}
The biggest advantage is that Object.create
will always work. Using new Animal()
only works if the constructor does not expect any arguments. Imagine if the constructor looked like this:
function Animal(name) {
this.name = name.toLowerCase();
}
You always have to pass a string to Animal
, otherwise you will get an error. What will you pass when you do Dog.prototype = new Animal(??);
? It doesn't actually matter which string you pass, as long as pass something, which hopefully shows you that this is bad design.
Some say that Dog.prototype = Animal.prototype;
can also work. So now I'm totally confused
Everything that "adds" the properties from Animal.prototype
to Dog.prototype
will "work". But the solutions are of different quality. In this case here you will have the problem that any method you add to Dog.prototype
will also be added to Animal.prototype
.
Example:
Dog.prototype.bark = function() {
alert('bark');
};
Since Dog.prototype === Animal.prototype
, all Animal
instances have a method bark
now, which is certainly not what you want.
Object.create
(and even new Animal
) add one level of indirection to the inheritance by creating a new object which inherits from Animal.prototype
and that new object becomes Dog.prototype
.
Inheritance in ES6
ES6 introduces a new syntax to create constructor functions and prototype methods, which looks like this:
class Dog extends Animal {
bark() {
alert('bark');
}
}
This is more convenient than what I explained above, but as it turns out, extends
also uses an internal equivalent to Object.create
to setup inheritance. See steps 2 and 3 in the ES6 draft.
Which means that using Object.create(SuperClass.prototype)
is the "more correct" approach in ES5.