π JavaScript Functions Deep Dive #7: Factory Function vs Constructor Function

In JavaScript, there are two common patterns for creating multiple similar objects:
Factory Functions
Constructor Functions
Both help in object creation, but they differ in syntax, style, behavior, and flexibility.
In this article, weβll explore:
β What factory and constructor functions are
β Syntax differences and best practices
β Pros and cons of each
β Real-world usage examples
π§ͺ Why Use These Patterns?
JavaScript doesnβt have traditional classes (before ES6). Instead, we use functions to create objects β either:
Factory functions (normal functions that return objects), or
Constructor functions (functions used with the
new
keyword)
π 1. Factory Function β A Function That Returns an Object
πΉ Syntax:
function createUser(name, age) {
return {
name,
age,
greet() {
return `Hi, I'm ${this.name}`;
},
};
}
const user1 = createUser("Alice", 25);
console.log(user1.greet()); // Hi, I'm Alice
β Key Characteristics
Feature | Factory Function |
Uses this ? | β Not required |
Needs new keyword? | β No |
Explicit return? | β Yes |
Easy to mix functional logic? | β Yes |
Private variables? | β Easy to achieve via closure |
π Private Variables Example with Closure
function createCounter() {
let count = 0;
return {
increment() {
count++;
return count;
},
getCount() {
return count;
},
};
}
const counter = createCounter();
console.log(counter.increment()); // 1
console.log(counter.getCount()); // 1
β
count
is truly private β inaccessible from outside.
π·ββοΈ 2. Constructor Function β Uses this
and new
πΉ Syntax:
function User(name, age) {
this.name = name;
this.age = age;
this.greet = function () {
return `Hello, I'm ${this.name}`;
};
}
const user2 = new User("Bob", 30);
console.log(user2.greet()); // Hello, I'm Bob
β Key Characteristics
Feature | Constructor Function |
Uses this ? | β Required |
Needs new keyword? | β Yes |
return not needed | β Object is returned automatically |
Prototype-friendly? | β Yes (memory efficient) |
π§ Behind the Scenes of new
When you call a constructor with new
:
A new object
{}
is created.this
inside the function refers to that object.Properties are attached to
this
.The object is returned automatically.
π‘ When to Use What?
Situation | Preferred Pattern |
You need private state | β Factory Function |
You want to leverage prototypes for memory | β Constructor |
You like FP style / React | β Factory |
You build utility object creators | β Factory |
Youβre building class-like hierarchy pre-ES6 | β Constructor |
π§βπ» Real-World Example
πΉ Factory Example
function createProduct(name, price) {
return {
name,
price,
discount(percent) {
return price * (1 - percent / 100);
},
};
}
πΉ Constructor Example
function Product(name, price) {
this.name = name;
this.price = price;
}
Product.prototype.discount = function (percent) {
return this.price * (1 - percent / 100);
};
π Summary
Feature | Factory | Constructor |
Return object? | Yes (explicit) | Yes (implicit) |
Use this ? | No | Yes |
Use new ? | No | Yes |
Easy privacy? | β Yes (closures) | β No |
Memory efficient with methods? | β No | β Yes (prototypes) |
β Final Thoughts
Both factory and constructor functions are valid ways to create objects in JavaScript. The best choice depends on:
Your teamβs preference (OOP vs functional)
Performance requirements
Code readability and privacy needs
In modern JavaScript, factory functions are often favored due to their simplicity and composability β especially in React, testing utilities, and pure functions.
Subscribe to my newsletter
Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
