Introduction
A constructor function allows you to create multiple objects with the same structure using a template. It is one of the foundational concepts of object-oriented programming (OOP) in JavaScript.
In this guide, you’ll learn everything about JavaScript constructor functions, including syntax, usage, examples, real-life applications, common mistakes, and interview questions.
What is a Constructor Function?
A constructor function is a special type of function used to create and initialize objects.
- First character of the constructor function should be in Capital letters.
- Constructor function is used to create the Object through new keyword.
Syntax of Constructor Function
function ConstructorName(parameters) {
this.property = value;
}
Basic Example
function Person(name, age) {
this.name = name;
this.age = age;
}
const user1 = new Person("John", 35);
const user2 = new Person("Tom", 30);
console.log(user1.name);
console.log(user2.name);
Output:
Tom
How new Keyword Works
When you use new, JavaScript performs the following steps:
- Creates a new empty object
- Sets this to the new object
- Links the object to the constructor’s prototype
- Returns the object
Example:
function Car(brand) {
this.brand = brand;
}
const car1 = new Car("Toyota");
console.log(car1.brand);
Output:
Adding Method in Constructor function
Now you want to add method fullName() in the constructor function.
Example:
function Employee(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
this.fullName= function(){
return this.firstName+" "+this.lastName;
}
}
const emp1 = new Employee("John", "Taylor");
console.log(emp1.firstName);
console.log(emp1.lastName);
Output:
Taylor
Real-Life Examples: Create User Object
function User(username, email) {
this.username = username;
this.email = email;
}
const user = new User("john123", "john@test.com");
console.log(user.username);
console.log(user.email);
Output:
john@test.com
Constructor vs Object Literal
| Feature | Constructor Function | Object Literal |
|---|---|---|
| Reusability | High | Low |
| Multiple Objects | Yes | No |
| Structure | Template | Fixed |
Common Mistakes
I will show you common mistakes, which mostly happens by Beginner.
1. Forgetting new Keyword
function Person(name) {
this.name = name;
}
const p = Person("John");
Note: this will refer to global object.
2. Not Using Capital Letter
function person() {} // Not recommended
Constructor Function and Inheritance
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
return this.name + " makes a sound";
};
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
const dog = new Dog("Tomy");
console.log(dog.speak());
Output:
Javascript Constructor function – Interview Questions
Q 1: What is constructor function?
Q 2: How to call the constructor?
Q 3: Naming convention?
Q 4: What does this refer to?
Q 5: Can constructors return a value?
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 ______.
Conclusion
JavaScript constructor functions are a powerful way to create reusable object templates. They form the foundation of object-oriented programming in JavaScript and are essential for understanding how modern JavaScript classes work.
By mastering constructor functions, prototypes, and this, you can write more structured, efficient, and scalable JavaScript code.