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

How does the `Object.prototype.hasOwnProperty()` method work?

The hasOwnProperty() method is used to determine if an object has a specific property as its direct attribute/own property (not inherited from prototypes). It returns true if the object has the property, and false if it doesn't.

javascript
Difficulty: 4/10
Topics: property lookup, prototype chain, enumeration

Scenario Questions

0-2 years experience
  1. 1

    You need to copy only the object's own enumerable properties into a new object. How would you use hasOwnProperty in a loop to achieve this?

  2. 2

    If you call obj.hasOwnProperty('toString'), what will be the result when toString is inherited from Object.prototype? Explain why.

2-5 years experience
  1. 1

    We have a function that merges two configuration objects using a for...in loop. It started failing after adding a new prototype method to Object. How would you modify the loop to prevent pulling in prototype properties?

  2. 2

    During debugging, you notice that a JSON.stringify call is including unexpected keys from the object's prototype chain. How can hasOwnProperty help you filter those out, and what change would you make in the code?

5-8 years experience
  1. 1

    You are building a utility library that provides a deep clone function. Discuss how you would use hasOwnProperty to correctly traverse objects without copying prototype properties, and any performance considerations for large object graphs.

  2. 2

    In a high‑throughput server, you need to validate incoming payload objects against a whitelist of allowed fields. Explain how you would use hasOwnProperty to enforce that only own properties are checked, and what pitfalls to watch for with objects that may have overridden hasOwnProperty.

8+ years experience
  1. 1

    Your team is refactoring a legacy codebase that heavily relies on for...in loops across many modules. Propose a strategy to replace those loops with safer patterns using hasOwnProperty, considering cross‑team impact, testing, and backward compatibility.

  2. 2

    Design a linting rule or TypeScript utility that enforces the use of Object.prototype.hasOwnProperty.call(obj, prop) instead of obj.hasOwnProperty(prop). Explain why this is important in a large monorepo with mixed code styles.

Follow-up Questions

  • What would happen if an object’s prototype overrides hasOwnProperty?
  • Can you think of any edge cases where hasOwnProperty might give a false negative?
  • How does using Object.prototype.hasOwnProperty.call differ from calling the method directly on the object?