The syntax of JavaScript can be somewhat confusing. In JS, there isn't class inheritance but instance-based inheritance. An instance's parent is also called its prototype.
When you write instance.something
or instance['something']
, the JS engine looks at the instance to see if it has a member called something
. If it doesn't, then it looks at the instance's prototype. If that object doesn't have the member something
, it looks at the prototype's prototype, again and again until it finds the property or reaches an instance which inherits from null
. In that case it will simply return undefined
.
Functions have a special property called prototype
which is something else. The function's .prototype
is a simple object that has a constructor
property which refers back to the function itself. When you create an object with the new
keyword, that object's prototype will be set to the function's .prototype
property. This is where the confusion comes from: the .prototype
property of a constructor can be seen as the default prototype of all instances made with said constructor.
So when you add methods to a class by writing something like this:
MyClass.prototype.foo = function() {
alert('foo');
};
...you're actually storing the function in the prototype of all MyClass instances. When the JS engine looks at the instances of MyClass, it will look for the foo
member which it won't find. Then it will look at the instance's prototype which happens to be set to MyClass.prototype
and it will find the foo
member and fetch it.
It's important to make the difference between an instance's prototype and a function's .prototype
, but most people don't realize it. When they speak of a class's prototype, they're talking about MyClass.prototype
. The instance prototype is accessible via __proto__
in many browsers, but that's not a standard feature of JavaScript and shouldn't be used in your code.
Now let's look at the code you're using to simulate class inheritance.
SubClass.prototype = Object.create(BaseClass.prototype);
SubClass.prototype.constructor = SubClass;
Object.create(parent)
can be seen as a function that does this:
return {
__proto__ : parent
};
In other words, it creates a blank Object whose prototype is the passed object. Since all SubClass instances will inherit from Subclass.prototype
, replacing SubClass.prototype
with an object that inherits from BaseClass.prototype
makes sure all SubClass instances also inherit from BaseClass.

However, as I said earlier, the default .prototype
attribute of a function is an empty object whose .constructor
is set to the function itself. So by manually setting the .constructor
again, we're perfectly mimicking default prototype behavior. If we don't do that, then instance.constructor
will return the first defined .constructor
property down the prototype chain, which will be BaseClass
. It doesn't really change anything in terms of behavior unless our code actually depends on the constructor
property, but it's safer to have it.
As a final note, like others mentioned before I could finally post this answer, you can't just do SubClass.prototype = BaseClass.prototype;
because then you wouldn't be able to add methods to the SubClass without adding them to the BaseClass.