There are four ways to create an object in JavaScript. They are as follows:
- Object as literal
- Constructor Invocation Pattern
- Object.create() method
- Using class after ES6
Implementation of Inheritance varies according to the object creation method. In this post, I am going to explain creating inheritance in between a function constructor.
Let’s say you have a function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function animal(name, age) { | |
this.name = name; | |
this.age = age; | |
} |
If you call the animal function using new operator, an object will be created. This way of object creation is also known as “Constructor Invocation Pattern”
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dog = new animal('foo', 5); | |
console.log(dog); | |
var cat = new animal('koo', 3); | |
console.log(cat); |
Object dog and cat both have their own names and age properties. If you want a property or method to be shared across all objects, add that to the prototype of the function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animal.prototype.canRun = function () { | |
console.log('yes ' + this.name + ' can run !'); | |
} |
Using the JavaScript prototype chain, both dog and cat objects can access the canRun method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dog = new animal('foo', 5); | |
dog.canRun(); // yes foo can run | |
var cat = new animal('koo', 3); | |
cat.canRun(); // yes koo can run |
Next, let us create another constructor – human:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function human(name, age, money) { | |
this.name = name ; | |
this.age = age ; | |
this.money = money; | |
} | |
human.prototype.canEarn = function () { | |
console.log('yes ' + this.name + 'can earn'); | |
} |
At this point in time, human and animal functions do not have any relationship. However, we know that human is also an animal. There are two problems with the human constructor.
- It has duplicate codes for name and age initialization. It should use animal constructor for this purpose.
- It does not have any link with animal constructor
The above said two problems can be removed by creating inheritance in between animal and human function constructors.
You can solve problem 1 of code duplication by modifying the human function as below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function human(name, age, money) { | |
animal.call(this, name, age); | |
this.money = money; | |
} |
Now, in human function, we are using the call method to manually pass a current object as a value of ‘this’ in animal function. This approach is also called Indirect Invocation Pattern. Now, an object instance for human can be created as shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var h1 = new human('dj', 30, '2000 $'); | |
console.log(h1); |
So far, we have solved problem 1 of code duplication; however, human function is still not linked to animal function. If you try to call canRun method on h1 object, JavaScript will throw you an error.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h1.canRun(); // throw error canRun is not a function |
You can fix this problem by linking the prototype of the human function with the prototype of the animal function constructor. There are two ways to do that.
- Using __proto__
- Using Object.create() method
You can link prototype of function constructors using Object.create() as shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
human.prototype = Object.create(animal.prototype); |
You can link prototype of function constructors using __proto__ as shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
human.prototype.__proto__ = animal.prototype; |
I would prefer the Object.create() method because__proto__ may not be supported in many browsers. After linking prototypes, in one way, you have created inheritance in between animal and human function constructors. Object instance of human can read all properties of animal function and can execute animal function methods.
Leave a Reply