Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Constructor

Introduction

In Javascript, Constructor is used to Create Object. You can create multiple objects by using same constructor.

Declaring Constructor

Syntax

function functionName(){ // constructor Properties & Methods }

Example

function identity () { this.firstName = 'John', this.lastName = 'Doe', this.age = 20, this.welcomeMessage = function(){ return("Hello" + " " + this.firstName); } }

Here,

We have only declared a Constructor that we will use later to create a Object.

Accessing Constructor

Once, We have declared a constructor we need to call or access the constructor

Syntax

const variableName = new constructorName();

Example

function identity () { this.firstName = 'John', this.lastName = 'Doe', this.age = 20, this.welcomeMessage = function(){ return("Hello" + " " + this.firstName); } } const person = new identity(); console.log(person.firstName); console.log(person.lastName); console.log(person.age); console.log(person.welcomeMessage()); // Output // John // Doe // 20 // Hello John

Here, We have Created a Constructor with Name identity then, we have created a new Object with name person

In the Console, person.firstName i.e. John will be Printed. , person.lastName i.e. Doe will be printed and person.age i.e. 20 will be printed.

It will also print welcomeMessage i.e. Hello John, it is a object method.

Now, Let me tell you why we should use constructor.

Because Once you have declared a constructor you can Create Multiple Objects.

For Example

function identity () { this.firstName = 'John', this.lastName = 'Doe', this.age = 20, this.welcomeMessage = function(){ return("Hello" + " " + this.firstName); } } const person1 = new identity(); console.log(person1.firstName); console.log(person1.lastName); console.log(person1.age); console.log(person1.welcomeMessage()); const person2 = new identity(); console.log(person2.firstName); console.log(person2.lastName); console.log(person2.age); console.log(person2.welcomeMessage()); // Output // John // Doe // 20 // Hello John // John // Doe // 20 // Hello John

Here, we have created 2 objects by using a same constructor.

Constructor With Function Parameters

In the Above Example you must be wondering what is the use of creating a multiple objects for same constructor.

But what if i tell you that you can create number of unique persons by using the Constructor, then it  creating multiple users will be boon for us.

Let's me tell you how we can achieve this.

function identity (idFirstName,idLastName, idAge) { this.firstName = idFirstName, this.lastName = idLastName, this.age = idAge, this.welcomeMessage = function(){ return("Hello" + " " + this.firstName); } } const person1 = new identity("John","Doe",20); console.log(person1.firstName); console.log(person1.lastName); console.log(person1.age); console.log(person1.welcomeMessage()); const person2 = new identity("Mark","Elmer",48); console.log(person2.firstName); console.log(person2.lastName); console.log(person2.age); console.log(person2.welcomeMessage()); // Output // John // Doe // 20 // Hello John // Mark // Elmer // 48 // Hello Mark

Here,

We have created multiple objects with unique parameter (firstName, lastName and age) by using single Constructor.

Adding Properties and Methods to Object.

function identity () { this.firstName = 'John', this.lastName = 'Doe', this.age = 20 } const person = new identity(); person.gender = "Male"; person.welcomeMessage = function(){ return("Hello" + " " + this.firstName); } console.log(person.firstName); console.log(person.lastName); console.log(person.age); console.log(person.gender); console.log(person.welcomeMessage()); // Output // John // Doe // 20 // Male // Hello John

Here,

In the constructor we have only declared firstName , lastName , age

Then, we have added gender and welcomeMessage to the Object person

You can add properties to the constructor in the same you add properties to the Objects.

Only properties can be added to the Constructor, if you want to add Methods to the Constructor then it can't be done.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?