15 / 18

What is the constructor function?

A constructor function in JavaScript is a special type of function that is used to create and initialize objects. It serves as a blueprint for creating objects of a specific type or class.

javascript
  1. 1

    Constructor functions are functions that are used to create objects with shared properties and behaviours. They are typically named with an initial capital letter and are used in conjunction with the new keyword to create instances of objects.

  2. 2

    Constructor functions are used to create and initialize objects. They serve as blueprints or templates for creating instances of objects with similar properties and behaviours.

  3. 3

    Constructor functions are often used to define custom data types or classes in JavaScript, especially in the context of object-oriented programming.

  4. 4

    Unlike object literal by using the constructor function we can define the shape of an object — the set of methods and the properties it can have — and then create as many objects as we like

Difficulty: 5/10
Topics: constructor functions, prototype inheritance, class migration

Scenario Questions

0-2 years experience
  1. 1

    We need a simple Person object using a constructor function. How would you write the function and create two instances with different names?

  2. 2

    What happens if you call a constructor function without the new keyword? Show the result with a short code example.

2-5 years experience
  1. 1

    Our codebase mixes ES5 constructor functions and ES6 classes. A bug appears where an instance created with a constructor function is missing a method defined on its prototype. How would you investigate and fix it?

  2. 2

    We want to add a static utility method to a constructor function. Demonstrate how to implement it and explain why it isn’t available on individual instances.

5-8 years experience
  1. 1

    We’re refactoring a large module that heavily uses constructor functions for data models. What trade‑offs should we weigh when converting them to ES6 classes, especially regarding inheritance behavior, performance, and existing instances?

  2. 2

    During a high‑traffic operation we notice a memory leak tied to objects created via a constructor function that captures closures. How would you pinpoint the leak and mitigate it at the component level?

8+ years experience
  1. 1

    Our organization plans to migrate all legacy modules that use constructor functions to a modern module system with classes and TypeScript. Propose an architectural migration strategy that minimizes disruption across multiple teams.

  2. 2

    How would you enforce a consistent object‑creation pattern (constructor functions vs factories vs classes) across several services, considering code ownership, linting rules, and runtime behavior?

Follow-up Questions

  • What does the prototype chain look like after you instantiate an object with a constructor function?
  • How would you convert a constructor function that uses inheritance into an ES6 class?
  • Can you describe a scenario where using a constructor function might be preferable to a factory function?