A constructor in JavaScript is a special function used to create and initialize objects. It allows you to define reusable object structures.
1οΈβ£ What is a Constructor?
A constructor function :
- Is a regular function but used with the
newkeyword. - Initializes an object and assigns properties to it.
- Typically follows PascalCase naming convention (
Person,Car,Employee).
Example of a Constructor Function
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 .thisinsidePersonrefers to the newly created object.- The function assigns properties (
nameandage) tothis. - The new object is returned automatically.
2οΈβ£ Constructor Without 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.
3οΈβ£ Constructor with Methods
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)
4οΈβ£ Constructor + Prototype (Efficient Approach)
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?
- Reduces memory usage by sharing methods among instances.
5οΈβ£ Constructor in ES6 Classes (Modern Approach)
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.
6οΈβ£ Checking Constructor Reference
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;
7οΈβ£ Custom Object Creation Without new
You 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?
newis not required.- Object literals are used instead of
this.
π Summary
| 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