Object.prototype.isPrototypeOf()

The isPrototypeOf() method tests for an object in another object's prototype chain.

Note: isPrototypeOf differs from the instanceof operator. In the expression "object instanceof AFunction", the object prototype chain is checked against AFunction.prototype, not against AFunction itself.

Syntax

JavaScript
<var>prototypeObj</var>.isPrototypeOf(<var>object</var>)

Parameters

object
The object whose prototype chain will be searched.

Return value

A Boolean indicating whether the calling object has another object in its prototype chain.

Description

The isPrototypeOf method allows you to check whether or not an object exists within another object's prototype chain.

For example, consider the following prototype chain:

JavaScript
function Fee() {
  // ...
}

function Fi() {
  // ...
}
Fi.prototype = new Fee();

function Fo() {
  // ...
}
Fo.prototype = new Fi();

function Fum() {
  // ...
}
Fum.prototype = new Fo();

Later on down the road, if you instantiate Fum and need to check if Fi's prototype exists within the Fum prototype chain, you could do this:

JavaScript
var fum = new Fum();
// ...

if (Fi.prototype.isPrototypeOf(fum)) {
  // do something safe
}

This, along with the instanceof operator particularly comes in handy if you have code that can only function when dealing with objects descended from a specific prototype chain, e.g., to guarantee that certain methods or properties will be present on that object.

Specifications

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
The definition of 'Object.prototype.hasOwnProperty' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Object.prototype.hasOwnProperty' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'Object.prototype.hasOwnProperty' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/object/isprototypeof

JavaScript Method Object Prototype