Questions
18 of 32
1What is the constructor function?
2What are Objects?
3What are the properties of an object?
4How does javascript implements object oriented programming?
5What are the key differences between javascript’s Object-based Inheritance and Conventional Class-based Inheritance?
6What is a prototype in JavaScript?
7How we can implement prototype-based inheritance?
8Explain the prototype chain and its role in property lookup.
9What is the difference between an object's `__proto__` and a constructor function's `prototype` property and [[Prototype]]?
10What are object wrappers for primitive type?
11Can you explain the use of object wrappers with examples?
12Explain how the `instanceof` operator works.
13What are different methods to create an object?
14What is an object initializer?
15What does the `Object.create()` method do?
16How are constructor functions used to create objects with shared properties and methods?
17Show an example of ES6 classes to create objects and inherit properties?
18What are factory functions in JavaScript ?
19What are Object constructors in JavaScript ?
20How can you add new properties and methods to the prototype of an existing constructor function?
21What is prototype pollution, and how can it be a security risk in JavaScript?
22Is modifying or extending built-in object prototypes recommended (e.g., adding a method to `Array.prototype`)? Why or why not?
23What happens when you try to access a property that doesn't exist on an object?
24How can you prevent or mitigate prototype pollution vulnerabilities in your code?
25How does the `Object.prototype.hasOwnProperty()` method work?
26How can you optimize prototype chain lookup for better performance?
27Can you explain how `Object.prototype.constructor` property works?
28What is the difference between modifying the prototype of an object and adding a property directly to the object?
29How does JavaScript handle circular references in object prototypes?
30What is static dispatching in object-oriented programming?
31What is the purpose of the `Object.keys()` method?
32Write a function flattenObject(obj) that flattens a deeply nested object into a single-level object, using dot notation for nested keys.
18 / 32

What are factory functions in JavaScript ?

  1. 1

    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.

  2. 2

    they do not require the use of the ‘this‘ keyword for inner values or the use of the ‘new‘ keyword when instantiating new objects.

  3. 3

    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.

  4. 4

    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.

javascript
Which Design Pattern does it follow?
  1. 1

    Factory functions follow the Factory Method Pattern (a Creational Pattern).

  2. 2

    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.

Why use this pattern?
  1. 1

    Decoupling: The rest of your app doesn't need to know how the object is built; it just needs the resulting object.

  2. 2

    Object Composition: Instead of deep inheritance chains (Dog -> Mammal -> Animal), a factory can 'compose' an object by mixing together different small behaviors.

  3. 3

    Favor Composition over Inheritance.

Difficulty: 5/10
Topics: factory pattern, object creation, dependency injection

Scenario Questions

0-2 years experience
  1. 1

    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?

  2. 2

    If you call your user factory function without the new keyword, what will happen and why?

2-5 years experience
  1. 1

    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.

  2. 2

    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?

5-8 years experience
  1. 1

    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?

  2. 2

    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?

8+ years experience
  1. 1

    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?

  2. 2

    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?

Follow-up Questions

  • Can you sketch a quick implementation of the factory?
  • What are the trade‑offs of using a factory versus a class?
  • How would you enforce a singleton if the factory should return the same instance?