Javascript Constructor function

Constructor function is used to create the Object through new keyword.
First character of the constructor function should be in Capital letters.

Syntax:-


function FunctionName(){
	
	// logic implement

}

var obj = new FunctionName();

Example:- Suppose, You have to create Employee() constructor function.


function Employee(){
	
this.firstName="John";
this.lastName="Mathew";
this.email="john@abc.com";
}

// Now, create the Object

var emp= new Employee();
console.log(emp.firstName);
console.log(emp.lastName);
console.log(emp.email);
Output:- John
Mathew
john@abc.com

Note:- this property reference to the constructor function.

Adding Method in Constructor function

Now you want to add method fullName() in the constructor function.


function Employee(){
     
this.firstName="John";

this.lastName="Mathew";

this.fullName= function(){
	
	return this.firstName+" "+this.lastName;
}

}

Now, create the object



var emp = new Employee();

console.log(emp.fullName())
Output:- John Mathew

Parameters pass in the constructor function

You can pass multiple parameters in the function according your requirements.

Syntax:-


function FunctionName(parameter_1, parameter_2,....parameter_n){
	
	// logic implement

}

var obj = new FunctionName(parameter_1, parameter_2,....parameter_n);

Example:-


function Employee(first_name, last_name){
	
	this.full_name="Welcome "+first_name+" "+last_name;

}

// Now, create the Object

var emp= new Employee("John","Mathew");
console.log(emp.full_name);
Output:- Welcome John Mathew

You can create multiple objects based on your requirements.


var emp= new Employee("Rom","Taylor");
console.log(emp.full_name);
Output:- Welcome Rom Taylor

Adding property into the constructor function object


function Employee(first_name, last_name){
     
this.firstName=first_name;

this.lastName=last_name;

this.fullName= function(){
	
	return this.firstName+" "+this.lastName;
}

}

Suppose, you have to add new property department in the constructor function employee.


var emp = new Employee("John","Mathew");
emp.department="Sales";
console.log(emp.department)
Output:- Sales

Now, you create another object and call the department property then it will show undefined.


var emp2 = new Employee("Rom","Taylor");
console.log(emp2.department)
Output:- undefined

Note:- If you add property after created the object then it will work for same object not other object.

Javascript Constructor function – Interview Questions

Q 1: What is constructor function?
Ans: Used to create objects.
Q 2: How to call the constructor?
Ans: Using new.
Q 3: Naming convention?
Ans: Capital letter.
Q 4: What does this refer to?
Ans: Newly created object.
Q 5: Can constructors return a value?
Ans: Objects only.

Javascript Constructor function – Objective Questions (MCQs)

Q1. Constructor functions are used to ______.






Q2. Which keyword is used to call a constructor function?






Q3. Constructor function names usually start with a ______.






Q4. Inside a constructor, this refers to ______.






Q5. Methods shared among objects are added to the ______.