Last Updated : 15 Feb, 2025
A constructor in JavaScript is a special function used to create and initialize objects. It sets up object properties and is typically invoked using the new keyword. Constructors allow for the creation of multiple instances with similar properties and methods.
In JavaScript, constructors can be defined in two main ways
Before ES6, JavaScript used constructor functions to create and initialize objects. A constructor function is essentially a regular function that is invoked using the new keyword. Here's how you define a constructor function.
JavaScript
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.getCarInfo = function () {
return `${this.year} ${this.make} ${this.model}`;
};
}
let myCar = new Car('Toyota', 'Camry', 2020);
console.log(myCar.getCarInfo());
With the introduction of ES6, JavaScript introduced class syntax, making object-oriented programming more structured and readable. Classes provide a more modern and organized way to define constructors.
JavaScript
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
getCarInfo() {
return `${this.year} ${this.make} ${this.model}`;
}
}
let myCar = new Car('Honda', 'Civic', 2022);
console.log(myCar.getCarInfo());
The new keyword in JavaScript is used to create an object out of a constructor function in a class representation and also from the normal function's.
JavaScript
//Object from a function
function prints(name,age)
{
this.name=name
this.age=age
}
const result=new prints('Pranjal',21)
console.log(result)
//Object from a class
class Person{
constructor(name,age)
{
this.name=name
this.age=age
}
}
const result1=new Person('Saurabh',30)
console.log(result1)
prints { name: 'Pranjal', age: 21 } Person { name: 'Saurabh', age: 30 }
In real life prototype means a copy of something same is the case in JavaScript here prototype means a copy of a object, function or class. Here in this scenario Object.create() is used to create a copy of a object and to copy all it's properties into itself without the help of constructor function.
JavaScript
const obj={
name:'Pranjal',
age:21
}
const obj1=Object.create(obj)
obj1.class='10th'
console.log(obj1.name)
A factory function is a regular function that returns a new object, making it a simple and reusable way to create multiple instances without using new.
JavaScript
function createPerson(name, age) {
return {
name: name,
age: age,
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
}
const person1 = createPerson("Pranjal", 30);
const person2 = createPerson("Vivekam", 25);
person1.greet();
person2.greet();
Hello, my name is Pranjal Hello, my name is Vivekam
A constructor with default values allows you to specify default values for properties if no values are provided during object creation. This helps in ensuring that the object always has valid properties, even if some arguments are missing.
JavaScript
function Person(name = "Unknown", age = 0) {
this.name = name;
this.age = age;
}
const person1 = new Person("Pranjal", 30);
const person2 = new Person("Amar");
const person3 = new Person();
console.log(person1);
console.log(person2);
console.log(person3);
Person { name: 'Pranjal', age: 30 } Person { name: 'Amar', age: 0 } Person { name: 'Unknown', age: 0 }
In JavaScript, you can add methods to the prototype of a constructor, allowing all instances created from that constructor to share the same method. This helps in optimizing memory usage, as the method is not recreated for each instance.
JavaScript
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
};
const person1 = new Person("Pranjal", 30);
const person2 = new Person("Amar", 25);
person1.greet();
person2.greet();
Hello, my name is Pranjal and I am 30 years old. Hello, my name is Amar and I am 25 years old.
This code demonstrates how inheritance in classes allows a child class to inherit both properties and methods from a parent class constructor. The child class (two) inherits the name property from the parent class (one) using the super() keyword.
JavaScript
class one{
constructor(name)
{
this.name=name
}
greet()
{
return `Hello ${this.name}`
}
}
class two extends one{
constructor(name,age)
{
super(name)
this.age=age
}
greet1(){
return `Your age is ${this.age}`
}
}
const person1=new one('Pranjal')
const person2=new two('Pranav',21)
console.log(person2.greet())
JavaScript constructors help developers create reusable, consistent, and well-structured objects. They simplify object initialization, support inheritance, and ensure cleaner code, making them essential in object-oriented programming. Understanding and using constructors effectively leads to more maintainable and efficient code in JavaScript.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4