Constructor in JavaScript


Reading time: 20 minutes | Coding time: 5 minutes

In this article, we are going to discuss about Constructor in Javascript to create an object. Javascript is a high level,weakly typed programming language. It is sometime abbreviated as JS. It is also a multi-paradigm (language that support more than one paradigm) and dynamic language.

Javascript offers various way to create the objects. Some of them are listed below:

  1. Creating Object Using Object Literal.
  2. Creating Object Using Constructor.
  3. Creating Object Using ES6 Class.

This is article is specifically about how to create Object using Constructor.

Unlike the Object Literal, which all to create one Object. But generally we need to create similar object like multiple user etc. This can be achieved using Constructor.

One need to define the constructor only once.And multiple object of same type can be created using the new operator.

Explanation of Constructor in Javascript

Constructor is nothing but simply a function with/without argument with property of class. An multiple Object of same/flavor can be constructed using new operator.

Convention to define Constructor in Javascript:-

1.It is quite similar to the function/method in javascript.It starts with function keyword followed by the name of the constructor which may or may not have arguments.

2.The property inside the constructor is defined using this keyword.An sample of code that use the this keyword to define property.

this.name = name;

Syntax:

function object_type( <parameters> )
{
    this.<object_property> = some parameters;
    // ...
}

// create object

let object = new object_type(parameters);

Let us look at a real life example to understand it better.

Implementation of the Code

function User(name) {
 this.name = name;
 this.isAdmin = false;
}

let user = new User("Jack");

alert(user.name); // Jack
alert(user.isAdmin); // false

Explanation of the above Code

In the above code,Constructor is defined having one argument name.Inside the constructor,two property name and isAdmin is defined. One important thing to note here is always this keyword is used to define the property.this always refer the particular instance of class that is created using constructor.

In the very next Line,a new instance user is created using the User constructor with the help of new operator.One can retreive the property of object with by using instance variable followed by . and its property name.

Method in Constructor

Constructor provides a very easy and efficient way of creating an object.this keyword is associated to define the properties of an object.this can be associated to define the method inside the constructor.

Syntax of defining a method with an object is:

function object_type( <parameters> )
{
    this.<object_property> = some parameters;
    // ...
    
    this.<function_name> = function() 
    {
        // do something with object properties
    }
}

// create object

let object = new object_type(parameters);

Let us look at a real life example to understand it better.

Implementation of the Code

function User(name) {
   this.name = name;

   this.sayHi = function() {
   alert( "My name is: " + this.name );
   };
}

let john = new User("John");

john.sayHi(); // My name is: John

/*
john = {
name: "John",
sayHi: function() { ... }
}
*/

Explanation of the Code

In the above code,A constructor User is defined.Inside the Constructor ,A method sayHi is defined.A method can be defined inside the constructor using this keyword followed by method name .

For invoking the method which is defined inside the constructor.First an instance of object is create using the constructor with the help of new operator,then use the instance variable followed by the method name.

Summary

  • Constructor provide a efficient and reusable method to create an object.Multiple instance of object can be created with help of constructor.

  • Object with the help of constructor is created using the new operator and this keyword is used to define the properties and method inside the constructor.