Difference between using the “new” keyword and Object.create() in JavaScript

Posted on Nov 10, 2014


Since this blog is at the moment somewhat new, it seems fitting to talk about creating new things. This post will be about the difference between using the new keyword and Object.create() in JavaScript.

Using new and using Object.create() are both similar, in that they allow you to extend the properties of one object with another.

For example, you might want to make some friends. Not in the illusory Facebook sort of way, but real friends that we can create ourselves by writing our own software. Because the only real friends in life are those we make ourselves.

First you create your Friend class with some basic properties that you want all of your friends to have initially. Generally you want human friends who speak some sort of English-like language (perfect English is not required) with a low meanness level.

Then you can add some methods that represent things you want your friends to do. These methods will be added using the prototype method during class creation. The prototype acts as a storage unit for the methods for this class.

If your friend smiles, their meanness level should go down. If they buy you a drink, their meanness level should go down even more.

Now you want a new type of friend with some specific attributes. You want your friend to have have everything that the basic friend has, but with some additional benefits.

You need to create the class for this new type of friend. This friend has a high generosity level and also owes you for the couple of times you have helped this friend move. Also, good friends always naturally have an abundance of overly priced beer in the fridge.

Since you want your friend to inherit the qualities of your basic friend, you can extend the GoodFriend class with the properties of the Friend class. We will be using the Object.create() method. This may be preferable to using the new keyword to extend a class, because the new keyword will invoke constructor method of the parent class and automatically apply properties to the subclass. This may or may not be desired behavior.

We will set the prototype of the GoodFriend class to be extended by the prototype of the Friend class:

In JavaScript, we also need to specify that we want the constructor of the subclass to point to itself. Otherwise it will rely on the constructor of the parent class.

Now some custom functionality for this friend. When that friend helps you move, their moving debt to you goes down.

As a good friend, they also let you drink their beer.

But since you are naturally a good friend too, you can buy them beer as well.

Now it’s time to actually make a new friend. We have an acquaintance named Sir Allan Sammy Boy, and a good friend named Carlton Christopher Coolidge Cumberbatch the Seventh (no relation to Benedict).

At this point, we will use the new keyword to create a new instance of the respective classes:

Voila! Now every time you want to drink some of Carlton’s beer, you can just do this:

Submit a Comment

Your email address will not be published. Required fields are marked *