×

JavaScript Objects

Object Creation using Constructor Function

A Constructor Function is a special function used to create and initialize objects. It allows values to be passed as parameters during object creation, making it easy to create multiple objects with different data.

Intermediate 20 Minutes Monopoly IT Solutions Updated 2026-08-03
Python class training banner
5.0
5.0
4.6
Overview

What you'll cover

Learning objectives

  • Understand Constructor Functions
  • Understand Parameterized Constructors
  • Create multiple objects
  • Understand private properties
  • Access object methods

Prerequisites

  • Variables
  • Functions
  • Objects
  • Object Literal
  • Object Constructor

Related topics

  • Object Literal
  • Object Constructor
  • Constructor Function (ES5)
  • Prototype
  • ES6 Classes
Theory

Concept breakdown

What is a Constructor?

A constructor is a function used to create and initialize objects.

Purpose of Constructor

Constructors initialize object properties and assign default or user-supplied values.

Parameterized Constructor

A constructor that receives values through parameters while creating an object.

Syntax
Hands-on example

Object Creation using Constructor Function

Console Output
> "Customer Id : 1001"
> "Customer Name : Sathesh"
> "Customer Amount : 1000"
> "Customer Id : 1002"
> "Customer Name : Ramesh"
> "Customer Amount : 2000"
Walkthrough

Step by step

  1. 1

    Define Constructor

    Create a Customer constructor function with three parameters.

  2. 2

    Initialize Properties

    Assign constructor parameters to private variables.

  3. 3

    Create Public Method

    Define showData() using the this keyword.

  4. 4

    Create Object

    Create the first object using the new keyword.

  5. 5

    Pass Values

    Pass customer details while creating the object.

  6. 6

    Display Data

    Invoke showData() to display customer information.

  7. 7

    Create Another Object

    Reuse the constructor to create another customer object with different values.

Summary

Key takeaways

Key points

  • Constructor functions act as blueprints for objects.
  • Objects are created using the new keyword.
  • Constructor parameters initialize object data.
  • Private properties are declared using local variables.
  • Public methods are created using the this keyword.
  • The same constructor can create multiple objects.

Advantages

  • Reusable code.
  • Easy to create multiple objects.
  • Supports encapsulation.
  • Initialization during object creation.
  • Reduces duplicate code.

Disadvantages

  • Each object gets its own copy of methods.
  • Consumes more memory than prototype methods.
  • ES6 Classes provide a cleaner syntax.
Real-world use

Customer Management System

Every customer record in a banking or shopping application can be represented as an object created using a constructor function.

Interview prep

Frequently asked questions

A Constructor Function is a function used to create and initialize objects.

The new keyword creates a new object and binds this to that object.

A constructor that receives values through parameters during object creation.

Yes. A single constructor function can create any number of objects.

Test yourself

Quick MCQ practice

1. Which keyword creates an object from a constructor function?

2. Constructor parameters are used to?

3. Which keyword is used to create public methods?

Apply it

Practice on your own

Coding exercise

Employee Constructor

Create an Employee constructor that accepts employeeId, employeeName and salary. Create a showDetails() method and create three employee objects.

Assignment

  1. Create a Product constructor.
  2. Accept productId, productName and price.
  3. Create showProduct() method.
  4. Create three product objects.
  5. Display all product details.

Tags

#javascript#constructor-function#parameterized-constructor#objects#oop

getintouch

Book for Live Demo!