Learn Factory Functions in JavaScript

Introduction

The Factory Function works like a model or blueprint to make similar objects or items. For example, if we want to create multiple car objects instead of individual car objects, we can use the factory function to create objects easily and remove duplicity.

What is a Factory Function?

A factory function is a function that creates and returns a new object each time it is called.

Let's take one example where we first create one object and then create two objects with the same structure without a factory function. So we can understand how much duplicate code we write for the same work.

Example1.js

let car1 = {
    make: 'Kia',
    model: 'Seltos',
    year: 2024,
    getCarInfo() {
      return `${this.make} ${this.model} (${this.year})`;
    },
  };
  
console.log(car1.getCarInfo()); 

Output

Factory Function

The above code creates the car1 object with the properties make, model, and year and one method getCarInfo that returns the car details. Now if we want to create another car object, we write the duplicate code as follows:.

Example2.js

let car1 = {
  make: 'Kia',
  model: 'Seltos',
  year: 2024,
  getCarInfo() {
    return `${this.make} ${this.model} (${this.year})`;
  },
};

console.log(car1.getCarInfo()); 

let car2 = {
    make: 'Tata',
    model: 'Punch',
    year: 2023,
    getCarInfo() {
      return `${this.make} ${this.model} (${this.year})`;
    },
  };
  
console.log(car2.getCarInfo()); 

Output

Factory Function

In the above code, you see we write duplicate code for the same work. Duplicating code is inefficient and irreverent, especially when dealing with many similar objects. Instead, you can use a factory function to streamline object creation.

Creating Objects with Factory Functions

For the same example above, we can generate many car objects without repeating the code.

Example3.js

function createCar(make, model, year) {
    return {
      make: make,
      model: model,
      year: year,
      getCarInfo() {
        return `${make} ${model} (${year})`;
      },
    };
  }
  
let car1 = createCar('Kia', 'Seltos', 2024);
let car2 = createCar('Tata', 'Punch', 2023);
let car3 = createCar('Maruti Suzuki', 'Ertiga', 2024);
let car4 = createCar('Honda', 'WR', 2020);
  
console.log(car1.getCarInfo()); 
console.log(car2.getCarInfo()); 
console.log(car3.getCarInfo()); 
console.log(car4.getCarInfo()); 

Output

Creating Objects with Factory Functions

In the above example, you can see that we have generated four objects without repeating the code. By using the createCar factory function, you can generate as many car objects as needed with different properties without duplicating the getCarInfo method.

Improving Memory Efficiency In Factory Function

Each car object created using the factory function has its own getCarInfo method. When a large number of objects are produced, this can result in significant memory utilization. To optimize memory usage, adopt a shared method approach.

Example4.js

function createCar(make, model, year) {
    return {
      make: make,
      model: model,
      year: year,
    };
  }
  
var carDetails = {
    getCarInfo() {
      return `${this.make} ${this.model} (${this.year})`;
    },
};
  
let car1 = createCar('Kia', 'Seltos', 2024);
let car2 = createCar('Tata', 'Punch', 2023);
  
car1.getCarInfo = carDetails.getCarInfo;
car2.getCarInfo = carDetails.getCarInfo;
  
console.log(car1.getCarInfo()); 
console.log(car2.getCarInfo()); 
  

Output

Improving Memory Efficiency In Factory Function

For a more efficient solution, we can use the Object.create() method to set up a prototype.

Example5.js

var carActions = {
    getCarInfo() {
      return `${this.make} ${this.model} (${this.year})`;
    },
  };
  
function createCar(make, model, year) {
    let car = Object.create(carActions);
    car.make = make;
    car.model = model;
    car.year = year;
    return car;
}
  
let car1 = createCar('Kia', 'Seltos', 2024);
let car2 = createCar('Tata', 'Punch', 2023);
  
console.log(car1.getCarInfo()); 
console.log(car2.getCarInfo()); 
  

Output

Improving Memory Efficiency In Factory Function

Using object.create() ensures that methods are shared between objects, resulting in better memory usage. Although factory functions are useful, class or constructor functions are often preferred for managing objects in modern JavaScript.

Summary

A factory function is a function that returns a new object. For better efficiency, use methods like Object.create() to share functionality across multiple objects. If you find this article valuable, please consider liking it and sharing your thoughts in the comments.

Thank you, and happy coding.


Similar Articles