mozilla

Revision 43388 of Object.prototype.isPrototypeOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/isPrototypeOf
  • Revision title: isPrototypeOf
  • Revision id: 43388
  • Created:
  • Creator: mimiz33
  • Is current revision? No
  • Comment Just a little modification for code being highlighted; one or more formatting changes

Revision Content

Summary

Tests for an object in another object's prototype chain.

Method of Object
Implemented in JavaScript ?
ECMAScript Edition ECMAScript 5th Edition

Syntax

prototype.isPrototypeOf(object)

Parameters

prototype
  an object to be tested against each link in the prototype chain of the object  argument

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

object
the object whose prototype chain will be searched

Description

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

For example, consider the following prototype chain:

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:

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.

See also

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/isPrototypeOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/isPrototypeOf" } ) }}

Revision Source

<h2 name="Summary">Summary</h2>
<p>Tests for an object in another object's prototype chain.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript ?</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h2 name="Syntax">Syntax</h2>
<p><code><em>prototype</em>.isPrototypeOf(<em>object</em>)</code></p>
<h2 name="Parameters">Parameters</h2>
<dl> <dt>prototype</dt> <dd>  an object to be tested against each link in the prototype chain of the <strong>object</strong>  argument</dd>
</dl>
<p>{{ Note("<code>isPrototypeOf</code> differs from <a href='\"en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/instanceof_Operator\"'>instanceof operator</a>. In the expression <code>object instanceof AFunction</code>, the <code>object</code> prototype chain is checked against <code>AFunction.prototype</code>, not against <code>AFunction</code> itself") }}</p>
<dl> <dt>object</dt> <dd>the object whose prototype chain will be searched</dd>
</dl>
<h2 name="Description">Description</h2>
<p><code>isPrototypeOf</code> allows you to check whether or not an object exists within another object's prototype chain.</p>
<p>For example, consider the following prototype chain:</p>
<pre class="brush: js">function Fee() {
  // . . .
}

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

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

function Fum() {
  // . . .
}
Fum.prototype = new Fo();
</pre>
<p>Later on down the road, if you instantiate <code>Fum</code> and need to check if <code>Fi</code>'s prototype exists within the <code>Fum</code> prototype chain, you could do this:</p>
<pre class="brush: js">var fum = new Fum();
. . .

if (Fi.prototype.isPrototypeOf(fum)) {
  // do something safe
}
</pre>
<p>This, along with the <code>instanceof</code> 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.</p>
<h2 name="See_also">See also</h2>
<ul> <li><a href="/en/JavaScript/Reference/Operators/instanceof" title="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/instanceof_Operator">instanceof</a></li> <li><a class="internal" href="/en/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf" title="En/Core JavaScript 1.5 Reference/Global Objects/Object/GetPrototypeOf">getProtoypeOf</a></li>
</ul>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/isPrototypeOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/isPrototypeOf" } ) }}</p>
Revert to this revision