DEV Community

Cover image for JavaScript Constructor Functions
Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

JavaScript Constructor Functions

What is a Constructor Function?

A Constructor Function is a regular JavaScript function used with the new keyword to create and initialize multiple objects with the same structure.

It acts like a blueprint for creating objects.

By convention, constructor function names start with a capital letter.

Syntax

function ConstructorName(parameter1, parameter2) {
    this.property1 = parameter1;
    this.property2 = parameter2;
}
Enter fullscreen mode Exit fullscreen mode

Example

function Employee(name, salary) {
    this.name = name;
    this.salary = salary;
}

const emp1 = new Employee("Saravanan", 50000);

console.log(emp1);
Enter fullscreen mode Exit fullscreen mode

Output

Employee {
    name: "Saravanan",
    salary: 50000
}
Enter fullscreen mode Exit fullscreen mode

How the new Keyword Works

When JavaScript executes:

const emp1 = new Employee("Saravanan", 50000);
Enter fullscreen mode Exit fullscreen mode

it performs the following steps automatically:

Step 1: Creates an Empty Object

{}
Enter fullscreen mode Exit fullscreen mode

Step 2: Sets this to the New Object

this = {}
Enter fullscreen mode Exit fullscreen mode

Step 3: Executes the Constructor Function

this.name = "Saravanan";
this.salary = 50000;
Enter fullscreen mode Exit fullscreen mode

Result:

{
    name: "Saravanan",
    salary: 50000
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Returns the Object Automatically

return this;
Enter fullscreen mode Exit fullscreen mode

Understanding this

Inside a constructor function, this refers to the newly created object.

function Employee(name) {
    this.name = name;
}

const emp1 = new Employee("Saravanan");
Enter fullscreen mode Exit fullscreen mode

Internally:

emp1.name = "Saravanan";
Enter fullscreen mode Exit fullscreen mode

The property name is on the left side and the value comes from the parameter on the right side.

this.name = name;
Enter fullscreen mode Exit fullscreen mode

Creating Multiple Objects

function Employee(name, salary) {
    this.name = name;
    this.salary = salary;
}

const emp1 = new Employee("Ram", 50000);
const emp2 = new Employee("Kumar", 60000);
const emp3 = new Employee("Ajay", 70000);
Enter fullscreen mode Exit fullscreen mode

All objects follow the same structure.


Adding Methods

Constructor functions can also contain methods.

function Employee(name, salary) {
    this.name = name;
    this.salary = salary;

    this.displayInfo = function() {
        console.log(this.name + " earns " + this.salary);
    };
}

const emp1 = new Employee("Saravanan", 50000);

emp1.displayInfo();
Enter fullscreen mode Exit fullscreen mode

Output

Saravanan earns 50000
Enter fullscreen mode Exit fullscreen mode

CRUD Operations on Constructor Function Objects

Objects created using constructor functions behave like normal JavaScript objects.

Create

emp1.age = 26;
Enter fullscreen mode Exit fullscreen mode

Read

console.log(emp1.age);
Enter fullscreen mode Exit fullscreen mode

Update

emp1.age = 27;
Enter fullscreen mode Exit fullscreen mode

Delete

delete emp1.age;
Enter fullscreen mode Exit fullscreen mode

Adding Properties to One Object

emp1.department = "IT";
Enter fullscreen mode Exit fullscreen mode

Only emp1 receives the property.


Adding Properties for All Objects Using Prototype

Employee.prototype.company = "TCS";
Enter fullscreen mode Exit fullscreen mode

Now all Employee objects can access the property.

console.log(emp1.company);
console.log(emp2.company);
Enter fullscreen mode Exit fullscreen mode

Output

TCS
TCS
Enter fullscreen mode Exit fullscreen mode

Adding Shared Methods Using Prototype

Employee.prototype.greet = function() {
    console.log("Hello " + this.name);
};
Enter fullscreen mode Exit fullscreen mode

Now every Employee object can use the same method.

emp1.greet();
emp2.greet();
Enter fullscreen mode Exit fullscreen mode

This approach is more memory-efficient because only one copy of the function exists.


Constructor Function vs Object Literal

Object Literal

const employee = {
    name: "Ram",
    salary: 50000
};
Enter fullscreen mode Exit fullscreen mode

Best when creating a single object.

Constructor Function

function Employee(name, salary) {
    this.name = name;
    this.salary = salary;
}
Enter fullscreen mode Exit fullscreen mode

Best when creating multiple objects with the same structure.


References:
https://www.w3schools.com/js/js_object_constructors.asp
https://www.geeksforgeeks.org/javascript/javascript-function-constructor/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function

Top comments (0)