Factory Functions in JavaScript

Home » Programming » Factory Functions in JavaScript

Factory Functions in JavaScript

Factory functions in JavaScript returns an object that can be reused for making multiple instances of the same object. It can additionally accept arguments permitting you to customize the returned object.

JavaScript factory functions have the same functionality that a class function or a constructor has, but instead of using the “new” keyword while creating an object instance it creates an object and returns it. They also do not require the use of the “this” keyword for accessing inner values.

In case we a have complex logic, and we have to create multiple objects again and again that have the same logic, we can write the logic once in a function and use that function as a factory to create our objects.

Usage of Factory Function in JavaScript

For example: we will create a “student1” object having two properties: “name” and “course”:

let student1 = {
    name: 'Amit',
    course: 'Computer Science' ,
    displayInfo() {
        return this.name + ' is a ' + this.course + ' student';
    }
};
console.log(student1.displayInfo());

We have added a method displayInfo() that will display the information of the object

 

student1-object

 

Now, let’s say you want to create another object “student2” for a different student. For this purpose, you have to write out the same code while changing the values of properties according to your requirements:

let student2 = { 
    name: 'Vijay', 
    course: 'Mechanical' , 
    displayInfo() { 
        return this.name + ' is a ' + this.course + ' student'; 
    } 
};

Student2 Object

 

The same process can be followed for other students also. But what if you want to create 1000 student objects? In such a case, creating each object separately while writing the same code again and again will consume a lot of time, effort, and can make your code complex to handle .

In such scenario you can use the factory function to avoid code duplication. It creates an object without diving into complex classes concepts.

Let’s create a JavaScript factory function “createStudent” for creating our student objects:

function createStudent(name, course) {
    return {
        name: name, 
        course: course , 
        displayInfo() { 
            return name + ' is a ' + course + ' student'; 
        }
    }
};

The above factory function will return an object comprising the property values passed as arguments.

 

Next step, we will create three student objects named as obj1, obj2, and obj3

let obj1 = createStudent('Amit', 'Computer Science');
let obj2 = createStudent('Vijay', 'Mechanical');
let obj3 = createStudent('Pratik', 'Electrical');

 

Next step, we will invoke the displayInfo() method for each object:

console.log(obj1.displayInfo());
console.log(obj2.displayInfo());
console.log(obj3.displayInfo());

 

Let’s execute this code:

Factory Function in JavaScript

 

Conclusion

In JavaScript, a Factory function is a type of function that returns an object and does not require:

  • the usage of new keyword.
  • the inner values can be accessed without the this keyword.

It can be used for initialising an object, similar to a constructor. It is also considered a useful tool that permits you to produce multiple object instances quickly.