What is Constructor Function in Javascript
In this article, we will see what is constructor function in Javascript. We know that object constructor and object literal notations are convenient for creating a single object. If we want to create multiple objects of the same type, then we take the help of the Constructor Function.
Before understanding the syntax of the constructor function and how we create objects using the constructor function, first I want to discuss why the constructor function.
Note :
Creating multiple objects of the same type using Object constructor and object literal notation:
- Increase code redundancy
- More time consuming
- Difficult to manage
Creating multiple objects of the same type using the constructor function:
- Reduces code redundancy
- Less time consuming
- Easy to manage
Example code for object literal notation
var rect1 = {
width:5,
height:5,
getArea:function(){
return this.width * this.height;
}
}
document.write(rect1.getArea())
// output: 25
var rect2 = {
width:6,
height:6,
getArea:function(){
return this.width * this.height;
}
}
document.write(rect2.getArea())
// output: 36
Steps:
- In the <script> tag, we have created the “rect1” object using the Object literal, rect means rectangle.
- In between the rect1 object flower brackets, we have properties and behavior.
- The “width” property has a value of 5 and “height” property has a value of 5. The “getArea” property has the value “function()” and returns the height/width value inside the function.
- And display the area of rect1 using document.write() method.
We have also created the rect2 object in the same way
Similarly, if we create many objects, our code redundancy will increase. So, we should use the constructor function for less code redundancy and time consumption. Now, we are going to create the same rect1 and rect2 using the constructor function.
Example code for Constructor function
function rectangle(width, height){
this.width = width;
this.height = height;
this.getArea= function(){
return this.width * this.height;
}
}
var rect1 = new rectangle(8,8);
var rect2 = new rectangle(5,9);
document.write(rect1.getArea(), "<br/>");
document.write(rect2.getArea());
/*
Output:
64
45
*/
Steps:
- Here, we have created a constructor function for all “rectangles”. First, we write the keyword “function” and then write the function name like ‘rectangle’.
- We have created a constructor function with the name “reactangle”. Every rectangle with have width and height, so we have passed the parameter (width, height) for the function.
- In the flower brackets of the function, the constructor function is responsible for constructing an object. We need to assign the user-supplied “width” value to the “width” parameter. So we need to use the “this” keyword to get the value given by the user. In the above, we write (this.width = width).
- Similarly, we need to assign the user-supplied “height” value to the “height” parameter, so we assign (this.width = width).
- Then “getArea” property has the value “function()” and returns the (this.height * this.width) value inside the function.
- Now, we created a constructor function. Using this constructor function, you can create any number of objects for a “rectangle”.
- Then we create an object called “rect1” with a width value of “8” and a height value of “8”. Then we create another object called “rect2” with different width and height values.
- And finally ,to display the width and height value of the rectangle, use (document.write) method and pass “objectname.getArea()” function inside it.
Conclusion
So, this is the advantage of the constructor function.