A constructor in JavaScript is a special function used to create and initialize objects. It allows you to define reusable object structures.
A constructor function :
new keyword.Person, Car, Employee).function Person(name, age) { this.name = name; this.age = age; } const person1 = new Person("Alice", 25); console.log(person1); // { name: "Alice", age: 25 }
π How it works:
new Person("Alice", 25) creates a new empty object .this inside Person refers to the newly created object.name and age) to this.new (Why new is Needed)If you forget to use new, this refers to the global object (window in browsers, global in Node.js).
const person2 = Person("Bob", 30); // No `new` used console.log(person2); // β undefined console.log(window.name); // β "Bob" (property assigned to global scope)
β
Always use new when calling a constructor function.
You can add methods inside the constructor, but it's inefficient because each instance gets a new copy of the method.
function Car(brand, model) { this.brand = brand; this.model = model; this.displayInfo = function () { console.log(`Car: ${this.brand} ${this.model}`); }; } const car1 = new Car("Toyota", "Camry"); const car2 = new Car("Honda", "Civic"); console.log(car1.displayInfo === car2.displayInfo); // β false (new function created for each instance)
Instead of defining methods inside the constructor, use prototype to share methods among all instances.
function Car(brand, model) { this.brand = brand; this.model = model; } // Add method to prototype (shared among all instances) Car.prototype.displayInfo = function () { console.log(`Car: ${this.brand} ${this.model}`); }; const car1 = new Car("Toyota", "Camry"); const car2 = new Car("Honda", "Civic"); console.log(car1.displayInfo === car2.displayInfo); // β true (shared method)
π Why use prototype?
ES6 introduces class syntax, making constructor-based object creation cleaner.
class Animal { constructor(name, type) { this.name = name; this.type = type; } speak() { console.log(`${this.name} says hello!`); } } const dog = new Animal("Buddy", "Dog"); dog.speak(); // Output: Buddy says hello!
β Same behavior as constructor functions but more readable.
β Uses prototype under the hood.
Each object instance retains a reference to its constructor.
console.log(car1.constructor === Car); // β true console.log(dog.constructor === Animal); // β true
This is why resetting constructor in prototype inheritance is necessary:
Employee.prototype.constructor = Employee;
newYou can simulate new by manually creating and returning an object.
function createPerson(name, age) { return { name, age, greet() { console.log(`Hi, I'm ${this.name}`); }, }; } const p1 = createPerson("Alice", 25); p1.greet();
π Difference?
new is not required.this.| Feature | Constructor Function | ES6 Class |
|---|---|---|
| Syntax | Function-based | classkeyword |
| Instantiation | newkeyword | newkeyword |
| Method Definition | Inside function (bad) or prototype (good) | Inside class body |
| Performance | Prototype-based (efficient) | Prototype-based (efficient) |
β Use ES6 classes for cleaner, modern syntax.
β Use prototype for shared methods to improve performance.
Test your understanding with 3 quick questions