Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Object.prototype.isPrototypeOf()

isPrototypeOf() 方法用于测试一个对象是否存在于另一个对象的原型链上。

: isPrototypeOf 和 instanceof operator 是不一样的。在表达式 object instanceof AFunction 中,检测的是 AFunction.prototype 是否在object 的原型链中,而不是检测 AFunction 自身。

语法

prototypeObj.isPrototypeOf(object)

参数

object
在该对象的原型链上搜寻

返回值

Boolean,表示调用对象是否在另一个对象的原型链上。

描述

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

例如,考虑下面的原型链:

function Fee() {
  // . . .
}

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

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

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

下面创建一个 Fum 实例,检测 Fi.prototype 是否存在于该实例的原型链上。

var fum = new Fum();
. . .

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

当需要判断对象的后代是否在特定原型链例如以保证一定的方法或属性将存在该对象上,这时候就需要用到 instanceof

规范

规范版本 规范状态 注解
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
Object.prototype.hasOwnProperty
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
Object.prototype.hasOwnProperty
Standard  
ECMAScript 2017 Draft (ECMA-262)
Object.prototype.hasOwnProperty
Draft  

浏览器兼容性

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)

相关链接

文档标签和贡献者

 此页面的贡献者: Ende93, helloguangxue, teoli, AlexChao, ziyunfei
 最后编辑者: Ende93,