The Object.isPrototypeOf() method in Javascript checks if an object exists in another object’s prototype chain.
Syntax:
prototypeObj.isPrototypeOf(object)
Parameters: This object accepts a single parameter.
- object: This is an object, whose prototype chain will be searched.
Return value: This method returns a boolean value.
Errors thrown: A Type Error is thrown if prototypeObj is undefined or null.
Example: This example shows the basic use of the JavaScript Object.prototype.isPrototypeOf() method.
javascript
function obj1() { } function obj2() { } obj1.prototype = Object.create(obj2.prototype); const obj3 = new obj1(); console.log(obj1.prototype.isPrototypeOf(obj3)); console.log(obj2.prototype.isPrototypeOf(obj3)); |
Output:
true true
Example 2: This example illustrates that C.prototype, B.prototype, A.prototype, and Object.prototype exist in the prototype chain for object c:
javascript
function A() { } function B() { } function C() { } B.prototype = Object.create(A.prototype); C.prototype = Object.create(B.prototype); let c = new C(); console.log(C.prototype.isPrototypeOf(c)); console.log(B.prototype.isPrototypeOf(c)); console.log(A.prototype.isPrototypeOf(c)); console.log(Object.prototype.isPrototypeOf(c)); |
Output:
true true true true
We have a complete list of Javascript Object Methods, to check those please go through the Javascript Object Complete Reference article.
Supported Browser:
- Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer-9 and above
- Opera 4 and above
- Safari 3 and above