The Factory Function is similar to constructor functions/class functions, but instead of using new to create an object, factory functions simply creates an object and returns it.
they do not require the use of the ‘this‘ keyword for inner values or the use of the ‘new‘ keyword when instantiating new objects.
Factory Functions can contain inner values, methods, etc. just like normal regular functions. Factory Functions differ from regular functions as they always return an object, which will contain any value, method, etc.
If we 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. It’s exactly the same as a real-world factory producing products.
Factory functions follow the Factory Method Pattern (a Creational Pattern).
Instead of the client (the code using the object) calling new and knowing the specific class details, it calls a 'Factory' and asks for an object. The Factory handles the complexity of creation.
Decoupling: The rest of your app doesn't need to know how the object is built; it just needs the resulting object.
Object Composition: Instead of deep inheritance chains (Dog -> Mammal -> Animal), a factory can 'compose' an object by mixing together different small behaviors.
Favor Composition over Inheritance.
We need a simple object representing a user with name and role. How would you write a factory function to create such objects, and how would you use it?
If you call your user factory function without the new keyword, what will happen and why?
Our team has a module that creates API client objects with configurable base URLs. We currently use a class constructor, but we want to switch to a factory function to simplify mocking in tests. Walk me through how you'd refactor it and what trade‑offs you consider.
During a bug hunt we noticed that two different parts of the code are getting different instances of a logger created by a factory function, causing duplicate log entries. How would you debug and fix this issue?
We have a high‑traffic service that creates thousands of request handler objects per second using a factory function. What performance considerations would you evaluate, and how might you redesign the factory to reduce GC pressure?
Our micro‑frontend architecture shares a common UI component library. Some components are instantiated via factories that also inject feature flags. How would you design the factory to be extensible across teams while avoiding circular dependencies?
The company is planning to replace a legacy codebase that heavily uses constructor functions with a modern factory‑based approach to improve testability and tree‑shaking. What architectural guidelines would you set, and how would you manage the migration across multiple product teams?
Imagine you need to expose a public API library where consumers can create objects via factories, but you also need to support plugin extensions that can augment those objects. How would you structure the factory system to allow safe, versioned extensions without breaking existing clients?