Questions
1 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.
01 / 32

What is the constructor function?

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

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

Scenario Questions

0-2 years experience

  1. 1We need a simple `Person` object using a constructor function. How would you write the function and create two instances with different names?
  2. 2What happens if you call a constructor function without the `new` keyword? Show the result with a short code example.

2-5 years experience

  1. 1Our 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. 2We 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. 1We’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. 2During 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. 1Our 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. 2How 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?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.