JavaScript(JS) object method - isPrototypeOf

www.‮tfigi‬idea.com

The isPrototypeOf() method is a built-in method of the JavaScript Object constructor. It checks whether an object exists in another object's prototype chain.

Here's the syntax:

prototypeObj.isPrototypeOf(obj)

where prototypeObj is the object to be checked whether it exists in the prototype chain of obj.

Here's an example that shows how to use the isPrototypeOf() method:

const myObj = {};
const myArray = [];

console.log(Object.prototype.isPrototypeOf(myObj)); // true
console.log(Array.prototype.isPrototypeOf(myArray)); // true
console.log(Object.prototype.isPrototypeOf(myArray)); // true
console.log(myObj.isPrototypeOf(myArray)); // false

In this example, we first create an empty object myObj and an empty array myArray. We then use the isPrototypeOf() method to check whether the Object.prototype and Array.prototype objects exist in the prototype chain of myObj and myArray.

Since myObj does not inherit from Array.prototype, the last console.log() statement returns false.

The isPrototypeOf() method is useful for checking whether an object inherits from another object. It can be used to determine whether a particular property or method is available in an object's prototype chain.