Object.prototype.isPrototypeOf()

Object 实例的 isPrototypeOf() 方法检查该对象是否存在于另一个对象的原型链中。

¥The isPrototypeOf() method of Object instances checks if this object exists in another object's prototype chain.

注意:isPrototypeOf()instanceof 运算符不同。在表达式 object instanceof AFunction 中,object 的原型链是针对 AFunction.prototype 进行检查,而不是针对 AFunction 本身进行检查。

¥Note: isPrototypeOf() differs from the instanceof operator. In the expression object instanceof AFunction, object's prototype chain is checked against AFunction.prototype, not against AFunction itself.

Try it

语法

¥Syntax

js
isPrototypeOf(object)

参数

¥Parameters

object

将搜索其原型链的对象。

返回值

¥Return value

一个布尔值,指示调用对象(this)是否位于 object 的原型链中。当 object 不是对象(即基元)时,直接返回 false

¥A boolean indicating whether the calling object (this) lies in the prototype chain of object. Directly returns false when object is not an object (i.e. a primitive).

例外情况

¥Exceptions

TypeError

如果 thisnullundefined,则抛出(因为它不可能是 转换为对象)。

描述

¥Description

所有从 Object.prototype 继承的对象(即除 null-原型对象 之外的所有对象)都继承 isPrototypeOf() 方法。此方法允许你检查该对象是否存在于另一个对象的原型链中。如果作为参数传递的 object 不是对象(即基元),则该方法直接返回 false。否则,this 值为 转换为对象,在 object 的原型链中查找 this 值,直到到达链尾或找到 this 值。

¥All objects that inherit from Object.prototype (that is, all except null-prototype objects) inherit the isPrototypeOf() method. This method allows you to check whether or not the object exists within another object's prototype chain. If the object passed as the parameter is not an object (i.e. a primitive), the method directly returns false. Otherwise, the this value is converted to an object, and the prototype chain of object is searched for the this value, until the end of the chain is reached or the this value is found.

示例

¥Examples

使用 isPrototypeOf()

¥Using isPrototypeOf()

此示例演示了对象 baz 的原型链中存在 Baz.prototypeBar.prototypeFoo.prototypeObject.prototype

¥This example demonstrates that Baz.prototype, Bar.prototype, Foo.prototype and Object.prototype exist in the prototype chain for object baz:

js
class Foo {}
class Bar extends Foo {}
class Baz extends Bar {}

const foo = new Foo();
const bar = new Bar();
const baz = new Baz();

// prototype chains:
// foo: Foo --> Object
// bar: Bar --> Foo --> Object
// baz: Baz --> Bar --> Foo --> Object
console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Baz.prototype.isPrototypeOf(bar)); // false
console.log(Baz.prototype.isPrototypeOf(foo)); // false
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(foo)); // false
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(bar)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true

如果你的代码只能在处理特定原型链的后代对象时起作用,那么 isPrototypeOf() 方法 - 以及 instanceof 运算符 - 就会特别方便;例如,保证该对象上存在某些方法或属性。

¥The isPrototypeOf() method — along with the instanceof operator — comes in particularly 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.

例如,要执行一些仅在 baz 对象的原型链中有 Foo.prototype 时才可以安全运行的代码,你可以这样做:

¥For example, to execute some code that's only safe to run if a baz object has Foo.prototype in its prototype chain, you can do this:

js
if (Foo.prototype.isPrototypeOf(baz)) {
  // do something safe
}

然而,baz 原型链中存在 Foo.prototype 并不意味着 baz 是使用 Foo 作为其构造函数创建的。例如,baz 可以直接指定为 Foo.prototype 作为其原型。在这种情况下,如果你的代码从 baz 读取 私有字段 of Foo,它仍然会失败:

¥However, Foo.prototype existing in baz's prototype chain doesn't imply baz was created using Foo as its constructor. For example, baz could be directly assigned with Foo.prototype as its prototype. In this case, if your code reads private fields of Foo from baz, it would still fail:

js
class Foo {
  #value = "foo";
  static getValue(x) {
    return x.#value;
  }
}

const baz = { __proto__: Foo.prototype };

if (Foo.prototype.isPrototypeOf(baz)) {
  console.log(Foo.getValue(baz)); // TypeError: Cannot read private member #value from an object whose class did not declare it
}

这同样适用于 instanceof。如果你需要以安全的方式读取私有字段,请改为使用 in 提供品牌检查方法。

¥The same applies to instanceof. If you need to read private fields in a secure way, offer a branded check method using in instead.

js
class Foo {
  #value = "foo";
  static getValue(x) {
    return x.#value;
  }
  static isFoo(x) {
    return #value in x;
  }
}

const baz = { __proto__: Foo.prototype };

if (Foo.isFoo(baz)) {
  // Doesn't run, because baz is not a Foo
  console.log(Foo.getValue(baz));
}

规范

Specification
ECMAScript Language Specification
# sec-object.prototype.isprototypeof

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看