What is a Factory Function? 🔖

Updated 1 year ago

I didn’t know this factory function technique before reading Kyle’s post and must say I find it a really nice alternative to writing classes.

I found a good use case for this technique today while enhancing blocks pulled from the Notion API with extra data and helper methods (more on that later), so I wanted to give Kyle a shout-out for the timely assist.

A factory function is a function that returns a new object. The key feature of a factory is that its only job is to pump out those items, just like an actual factory.
The place I use factories most often is when I want to return an object with methods but use closures to create private methods and values. Even better, I never have to even think about the this keyword.

Source: What is a Factory Function? by Kyle Shevlin