DEV Community 👩‍💻👨‍💻

Osinachi Chukwujama
Osinachi Chukwujama

Posted on • Updated on

Exploring Factory Functions

Looking at the words, factory and function would paint a picture of a function which creates objects. That's exactly what factory functions do. They are blueprints for object creation. More like classes. But milder, without the syntactic sugar.

Factory functions allow for speedy object creation by just calling the function with a few parameters here and there. Say we want a Sims factory. We would need a function which outputs Sims with the following properties:

  1. Name
  2. Age
  3. Gender

The factory

const makeSim = (name, age, gender) => {
  return {
Enter fullscreen mode Exit fullscreen mode

The function simply returns an object.
Now that we have that in place, we can add methods to this object.

const makeSim = (name, age, gender) => {
  return {
    get name() {return this._name},
    get age() {return this._age},
    get gender() {return this._gender}
const Sam = makeSim('Samuel', 23, 'male');
console.log(Sam);    // outputs {name: "Sam", age: 23, gender: "male"}
console.log(;    //outputs Samuel
Enter fullscreen mode Exit fullscreen mode

So that's my little say on factory functions. Post your views on the topic in the comment section. Thanks.

Top comments (0)


Thank you.

Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.