Prototypes and Inheritance in JavaScript

By Alex Mungai Muchiri, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

JavaScript is a language that uses prototypes very widely to share methods and properties of objects. As a consequence, it is very easy to clone and extend generalized objects, a method that is referred to as prototypical inheritance. Notably, it is a very different technique from the class inheritance and should not be confused. Most object-oriented programming languages such as PHP, Python, and Java are class-based, a stark difference from the prototypical inheritance of JavaScript. Those languages usually have object blueprints for classes. This article is dedicated for the understanding of JavaScript object prototypes, their inheritance and chains. You will also learn how constructors are used to extend objects with new prototypes.

Prototypes in JavaScript

It is assumed that you already are acquainted with object data types in JS, creating objects and how to modify object properties. Prototypes allow us to extend objects based on the fact that each JavaScript object includes [[Prototype]] as an internal property. To see this in action, let us create a new empty project like so:

this is a practical method that is used to create objects, but there is also another approach that uses the object constructor like so:

The property [[Prototype]] has double square brackets, which is an indicator of its internal nature, that is, it is not accessible directly in code. Accordingly, we need to use the getPrototypeOf() method to obtain[[Prototype]] of the object.

The method yields the following built-in properties and methods

It is also plausible to use proto property to examine the[[Prototype]]. The proto method also yields an object’s internal properties.

Please do note that proto should not be used in production code since it is a legacy feature that is not universally compatible with all available browsers. Nonetheless, for purposes of this tutorial, we can still use it for demonstration but keep the previous sentiment in mind.

we would still get the same output as when we make use of the getPrototypeOf().

Ideally, every object in JavaScript should have a [[Prototype]] so that it allows for the linking of two or ore objects.

All JavaScript objects created or built-in objects consist of a [[Prototype]] property. Using the internal property, we can then refer to objects between each other, as has been explored further down, using prototype property.


JavaScript initially searches on an object whenever you attempt to access its property or method, and if not found, it then proceeds to in its [[Prototype]]. If there still are no matches for the property at the [[Prototype]], JavaScript then tries checking from the linked object’s prototype until all objects in the chain are all covered n the search. Each chain contains Object.prototype at the end, whereby, there is an inheritance of methods and properties from Object. Searching beyond the chain always returns a null output. In our case above we have an empty object x, that bears inheritance from Object. If Object has any associated properties and methods, such as toString(), they all could be used x. for instance, we could have:

This is a prototype chain with only one link that could be defined as x -> Object. We can verify if there are any other links by chaining [[Prototype]] properties twice. If there are no other chains, the output is null.

we are now going to explore a different type of object. Specifically, if you have already encountered JavaScript Arrays, you must be familiar with pop() and push(), which are other built-in methods. These two methods are always accessible when new arrays are created. The reason that is so is due to the inherent accessibility of the Array.prototype by all newly created arrays. Use the line below to create a new array:

there is also the method of using array constructors to create new arrays that is accomplished like so:

Let us now examine the new array’s [[Prototype]] like so, and it will be evident that it contains far more properties and methods compared to the x object since it inherits all properties associated with Array.prototype.

There is a prototype’s constructor property on our array, which is vividly set to Array(). What it does is to return an object’s constructor function, a mechanism that allows the use of functions to construct objects.

It is further possible to chain two prototypes in our longer chain into such syntax as: y -> Array -> Object.

When the Array.prototype is examined, we get the following

It is observed that Object.prototype is referred to in the chain. We could perform a further test on the internal [[Prototype]] and compared it with the constructor function’s prototype property to determine if they yield the same result:

The isPrototypeOf() method could also be applied here

An operator’s instanceof is a method that could be used to test the appearance of a constructor’s prototype property within the prototype chain as indicated below:

In summary, we have determined that there is a hidden internal [[Prototype]] property in all JavaScript objects. Such properties are accessible using the proto method. We can extend objects due to the underlying fact that the could be extended through inheritance of their constructor’s [[Prototype]] properties and methods. When chained, all objects added to the chain inherit the properties and methods to the end of the chain, which ends with Object.prototype.

Constructor Functions

Essentially, constructor functions create new JavaScript objects. Launching new instances that base on constructor functions, we use new operator. However, JavaScript also contains built-in constructors like new Array() and new Date(), but is also possible to create new ones that we the use create objects.

For instance, we could be trying to create a simple role-play game that is based on text. In our game, a player shall choose a character and the associated class such as doctor, soldier, robber, and so on.

All our game characters have several aspects in common such as name, level and points, which means that it is preferable to have a common constructor for characters. At the same time, each character shall be limited to accessing only its own abilities, which vary between the characters. We are going to use prototype inheritance and constructor functions to attain out objective.

As a start, do not that constructor functions are like any other regular functions. The key differentiator between the two is that constructors are called on by instances with new key words. It is a JavaScript requirement that we capitalize a constructor function’s first letter. See example:

The example demonstrates the creation of a constructor function (Actor) consisting of the parameters name and level. All the characters of our text game have a name and an associated level, it would be ideal that they all share the properties contained in our constructor function above. The keyword this indicates new instances that have been created. Thus, for all objects to have a have a name property, should be set too the name parameter as has been shown in the example.

Let us try to build a new instance using new.

Now, let us try to console actor1, and observe if we have created a new object and what its properties would be:

Further, we examine the [[Prototype]] of actor1, which should output the constructor as Actor().

Do keep in mind that the actor1.__proto__ method would also yield the same result but like we mentioned, it has limited browser compatibility and therefore not very appropriate.

Our constructor only has properties defines as we are yet to define the methods. In JavaScript, we should define a prototype’s methods so that efficiency and code readability are improved.

Accordingly, let us now define a method for Actor using prototype. We are going to use the greet() method for demonstration.


Actor now has greet()method as part of the prototype. Accordingly, since hero1 constitutes an instance of Actor, actor1 should inherit the method as well:

If you need to, you can inspect Actor’s [[Prototype]] and you will find that greet() is now available. However, we still need to create character classes for our Actors. As we noted earlier, our game Actors will possess different abilities. It would thus be illogical if all the characters had their abilities contained in the Actor constructor, since they would all inherit the same abilities yet they should be different. How we approach the problem is that we create a new constructor function that is still connected to the original Actor.

We then proceed to export one constructor’s properties to another by use of the call() method. Let us create constructors for a soldier and a doctor:

As you can observe, we have created constructors that contain an Actor’s properties as well as a few additional ones. Now, we shall add new methods for our Soldier and Doctor. The attack() method applies to Soldier while the heal() method applies to Doctor.

Let us proceed to include the two classes in our characters

What is does in effect is as follows:

Recognise actor1 as a soldier and the added properties:

Let us try using the methods that we have introduced on the Soldier prototype.

However, if we try to use the second method we get the result below:

Evidently, the new methods are not automatically inherited by the call(). What we need is to link the prototypes is to use the Object.create(). However, it needs to be located before the methods we have created for our prototypes.

The Actor’s prototype methods have now been added to instances of Soldier or Doctor.

Below is the entire code for the character creation

What we have achieved in this tutorial is demonstrate the concept of prototypes and inheritance. We successfully created an Actor class that bears the base properties. We then created the Soldier and Doctor character classes inheriting from our initial constructor. Finally, we added to the prototypes new methods and built instances for each character.


Unlike traditional class-based programming languages, JavaScript is based on prototypes. We have explored its prototypes and learnt how to link objects into chains through [[Prototype]] property. We’ve also passed down properties of prototypes through the chain.

Don’t have an Alibaba Cloud account? Sign up for an account and try over 40 products for free worth up to $1200. Get Started with Alibaba Cloud to learn more.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.