Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is the Object.isPrototypeOf() method in Javascript?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The isPrototypeOf() method of the Object class in Javascript checks if an object is another object’s prototype. To determine the class of an object, the isPrototypeOf() method checks the entire prototype chain of the other object.

The process is illustrated below:

To use the isPrototypeOf() method, you will need to use the following command:

firstObj.isPrototypeOf(secondObj);

In the code shown above, firstObj and secondObj represent an instance of the Object class.

Parameters

The isPrototypeOf() method takes a single mandatory parameter, i.e., the object whose prototype chain must be checked.

Return value

The isPrototypeOf() method returns true if firstObj is the prototype of secondObj; otherwise, it returns false.

Example

The code below shows how the isPrototypeOf() method works in Javascript:

// initializing objects
var objOne = new Object();
function func(){};
var array = [5, 10, 20];
// checking valid prototype chains
console.log(Object.prototype.isPrototypeOf(objOne));
console.log(Function.prototype.isPrototypeOf(func));
console.log(Array.prototype.isPrototypeOf(array));
//checking invalid prototype chains
console.log(Function.prototype.isPrototypeOf(array));
console.log(Array.prototype.isPrototypeOf(objOne));

Explanation

First, the code initializes an empty object, function, and array. The isPrototypeOf() method is then used to evaluate the prototype chains of different combinations of classes and variables.

The first 33 calls to the isPrototypeOf() method all return true because the objects objOne, func, and array are prototypes of the Object, Function, and Array classes, respectively.

On the other hand, the isPrototypeOf() method in line 1212 returns false because array belongs to the Array class but not the Function class. Similarly, the isPrototypeOf() method in line 1313 also returns false, as objOne is not a Function prototype, but rather an Object.

RELATED TAGS

javascript

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring