isPrototypeOf()
method checks if an object exists in another object's prototype chain.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
isPrototypeOf()
differs from the
instanceof
operator. In the expression "
object instanceof AFunction
", the
object
prototype chain is checked against
AFunction.prototype
, not against
AFunction
本身。
prototypeObj.isPrototypeOf(object)
object
The object whose prototype chain will be searched.
A
布尔
indicating whether the calling object lies in the prototype chain of the specified object.
isPrototypeOf()
method allows you to check whether or not an object exists within another object's prototype chain.
This example demonstrates that
Baz.prototype
,
Bar.prototype
,
Foo.prototype
and
Object.prototype
exist in the prototype chain for object
baz
:
function Foo() {}
function Bar() {}
function Baz() {}
Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);
var baz = new Baz();
console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true
isPrototypeOf()
method, 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.
For example, check if
baz
object descends from
Foo.prototype
:
if (Foo.prototype.isPrototypeOf(baz)) {
// do something safe
}
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.prototype.isPrototypeOf' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
isPrototypeOf
|
Chrome 1 | Edge 12 | Firefox 1 | IE 9 | Opera 4 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function