instanceof
operator
tests to see if the
prototype
property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
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.
object instanceof constructor
object
The object to test.
构造函数
Function to test against
instanceof
operator tests the presence of
constructor.prototype
in
object
's prototype chain.
// defining constructors
function C() {}
function D() {}
let o = new C()
// true, because: Object.getPrototypeOf(o) === C.prototype
o instanceof C
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D
o instanceof Object // true, because:
C.prototype instanceof Object // true
C.prototype = {}
let o2 = new C()
o2 instanceof C // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C
D.prototype = new C() // add C to [[Prototype]] linkage of D
let o3 = new D()
o3 instanceof D // true
o3 instanceof C // true since C.prototype is now in o3's prototype chain
Note that the value of an
instanceof
test can change based on changes to the
prototype
property of constructors. It can also be changed by changing an object's prototype using
Object.setPrototypeOf
. It is also possible using the non-standard
__proto__
特性。
instanceof
and multiple context (e.g. frames or windows)
Different scopes have different execution environments. This means that they have different built-ins (different global object, different constructors, etc.). This may result in unexpected results. For instance,
[] instanceof window.frames[0].Array
will return
false
, because
Array.prototype !==
window.frames[0].Array
and arrays inherit from the former.
This may not make sense at first, but for scripts dealing with multiple frames or windows, and passing objects from one context to another via functions, this will be a valid and strong issue. For instance, you can securely check if a given object is, in fact, an Array using
Array.isArray(
myObj
)
For example, checking if a
节点
是
SVGElement
in a different context, you can use
myNode instanceof myNode.ownerDocument.defaultView.SVGElement
.
Note for Mozilla developers:
In code using XPCOM,
instanceof
has special effect:
obj
instanceof
xpcomInterface
(e.g.
Components.interfaces.nsIFile
) calls
obj
.QueryInterface(
xpcomInterface
)
并返回
true
if
QueryInterface
succeeded.
A side effect of such call is that you can use
xpcomInterface
's properties on
obj
after a successful
instanceof
test. Unlike standard JavaScript globals, the test
obj
instanceof
xpcomInterface
works as expected, even if
obj
is from a different scope.
String
and
Date
are of type
Object
and exceptional cases
The following code uses
instanceof
to demonstrate that
String
and
Date
objects are also of type
Object
(they are derived from
Object
).
However, objects created with the object literal notation are an exception here: Although the prototype is
undefined
,
instanceof Object
返回
true
.
let simpleStr = 'This is a simple string'
let myString = new String()
let newStr = new String('String created with constructor')
let myDate = new Date()
let myObj = {}
let myNonObj = Object.create(null)
simpleStr instanceof String // returns false, string literal is not an object
myString instanceof String // returns true
newStr instanceof String // returns true
myString instanceof Object // returns true
myObj instanceof Object // returns true, every object literal has Object.prototype as prototype
({}) instanceof Object // returns true, same case as above
myNonObj instanceof Object // returns false, prototype is end of prototype chain (null)
myString instanceof Date // returns false
myDate instanceof Date // returns true
myDate instanceof Object // returns true
myDate instanceof String // returns false
mycar
是类型
Car
and type
Object
The following code creates an object type
Car
and an instance of that object type,
mycar
。
instanceof
operator demonstrates that the
mycar
object is of type
Car
and of type
Object
.
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
let mycar = new Car('Honda', 'Accord', 1998)
let a = mycar instanceof Car // returns true
let b = mycar instanceof Object // returns true
To test if an object is not an
instanceof
a specific constructor, you can do
if (!(mycar instanceof Car)) {
// Do something, like:
// mycar = new Car(mycar)
}
This is really different from:
if (!mycar instanceof Car)
This will always be
false
. (
!mycar
will be evaluated before
instanceof
, so you always try to know if a boolean is an instance of
Car
).
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Relational Operators' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
instanceof
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持