isPrototypeOf

  • 版本网址缩略名: JavaScript/Reference/Global_Objects/Object/isPrototypeOf
  • 版本标题: isPrototypeOf
  • 版本 id: 343143
  • 创建于:
  • 创建者: ziyunfei
  • 是否是当前版本?
  • 评论

修订内容

概述

测试一个对象是否存在于另一个对象的原型链中.

Method of Object
Implemented in JavaScript ?
ECMAScript Edition ECMAScript 3rd Edition

语法

prototype.isPrototypeOf(object)

参数

prototype
  检查该对象是否在参数object的原型链上

{{ 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
在该对象的原型链上搜寻

描述

isPrototypeOf 方法允许您检查一个对象是否存在于另一个对象的原型链.

例如,如下的原型链:

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.

相关链接

{{ 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" ,"en":"en/JavaScript/Reference/Global_Objects/Object/isPrototypeOf"} ) }}

修订版来源

<h2 id="Summary" name="Summary">概述</h2>
<p>测试一个对象是否存在于另一个对象的原型链中.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/zh-cn/JavaScript/Reference/Global_Objects/Object" title="zh-cn/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 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">语法</h2>
<p><code><em>prototype</em>.isPrototypeOf(<em>object</em>)</code></p>
<h2 id="Parameters" name="Parameters">参数</h2>
<dl>
  <dt>
    prototype</dt>
  <dd>
    &nbsp; 检查该对象是否在参数object的原型链上</dd>
</dl>
<p>{{ Note("<code>isPrototypeOf</code> differs from <a href="\&quot;zh-cn/JavaScript/Reference/Operators/instanceof\&quot;">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>
    在该对象的原型链上搜寻</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p><span class="long_text short_text" id="result_box" lang="zh-CN"><span>isPrototypeOf</span><span> 方法允许您检查</span><span>一个对象</span><span>是否</span><span>存在于</span><span>另一个对象的</span><span>原型链</span><span>中</span></span>.</p>
<p>例如,如下的原型链:</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 id="See_also" name="See_also">相关链接</h2>
<ul>
  <li><a href="/zh-cn/JavaScript/Reference/Operators/instanceof" title="zh-cn/JavaScript/Reference/Operators/instanceof">instanceof</a></li>
  <li><a class="internal" href="/zh-cn/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf" title="zh-cn/JavaScript/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" ,"en":"en/JavaScript/Reference/Global_Objects/Object/isPrototypeOf"} ) }}</p>
恢复到这个版本