instanceof
2016-06-13 15:38
309 查看
The
whether an object has in its prototype chain the
The
prototype chain.
Note that if the value of an
of constructors, it cannot be changed by changing an object prototype, because changing an object prototype is not possible in standard ECMAScript. It is however possible using the non-standard
Different scope 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,
not make sense at first but when you start dealing with multiple frames or windows in your script and pass 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
Note for Mozilla developers:
In code using XPCOM
calls
returns
can use
properties on
Unlike standard JavaScript globals, the test
as expected even if
The following code uses
are also of type
However, objects created with the object literal notation are an exception here: Although the prototype is undefined,
The following code creates an object type
The
is of type
Desktop
Mobile
instanceofoperator tests
whether an object has in its prototype chain the
prototypeproperty of a constructor.
Syntax
object instanceof constructor
Parameters
objectThe object to test.
constructorFunction to test against
Description
The instanceofoperator tests presence of
constructor.prototypein
object's
prototype chain.
// defining constructors function C(){} function D(){} var 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 = {}; var 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(); // use inheritance var o3 = new D(); o3 instanceof D; // true o3 instanceof C; // true
Note that if the value of an
instanceoftest can change based on changes to the
prototypeproperty
of constructors, it cannot be changed by changing an object prototype, because changing an object prototype is not possible in standard ECMAScript. It is however possible using the non-standard
__proto__pseudo-property.
instanceof
and multiple context (e.g. frames or windows)
Different scope 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].Arraywill return
false, because
Array.prototype !==
window.frames[0].Arrayand arrays inherit from the former. This may
not make sense at first but when you start dealing with multiple frames or windows in your script and pass 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)
Note for Mozilla developers:
In code using XPCOM
instanceofhas special effect:
obj instanceof
xpcomInterface(e.g.
Components.interfaces.nsIFile)
calls
obj.QueryInterface(xpcomInterface)and
returns
trueif QueryInterface succeeded. A side effect of such call is that you
can use
xpcomInterface's
properties on
objafter a successful
instanceoftest.
Unlike standard JavaScript globals, the test
obj instanceof xpcomInterfaceworks
as expected even if
objis from a different scope.
Examples
Demonstrating that String
and Date
are
of type Object
and exceptional cases
The following code uses instanceofto demonstrate that
Stringand
Dateobjects
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 Objectreturns
true.
var simpleStr = "This is a simple string"; var myString = new String(); var newStr = new String("String created with constructor"); var myDate = new Date(); var myObj = {}; simpleStr instanceof String; // returns false, checks the prototype chain, finds undefined myString instanceof String; // returns true newStr instanceof String; // returns true myString instanceof Object; // returns true myObj instanceof Object; // returns true, despite an undefined prototype ({}) instanceof Object; // returns true, same case as above myString instanceof Date; // returns false myDate instanceof Date; // returns true myDate instanceof Object; // returns true myDate instanceof String; // returns false
Demonstrating that mycar
is of type Car
and
type Object
The following code creates an object type Carand an instance of that object type,
mycar.
The
instanceofoperator demonstrates that the
mycarobject
is of type
Carand of type
Object.
function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } var mycar = new Car("Honda", "Accord", 1998); var a = mycar instanceof Car; // returns true var b = mycar instanceof Object; // returns true
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2017 Draft (ECMA-262) The definition of 'Relational Operators' in that specification. | Draft | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Relational Operators' in that specification. | Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'The instanceof operator' in that specification. | Standard | |
ECMAScript 3rd Edition (ECMA-262) The definition of 'The instanceof operator' in that specification. | Standard | Initial definition. Implemented in JavaScript 1.4. |
Browser compatibility
Desktop Mobile
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
See also
typeof
相关文章推荐
- xshell如何正确来连接远程Linux服务器?
- 【leetcode】110. Balanced Binary Tree
- Grunt 之 watch 和 livereload
- 小孩钢琴练指
- 股市日记(一)
- 警察和厨师不可告人的秘密
- BootStrap 智能表单实战系列(十)自动完成组件的支持
- 2016.6.13 通过虚拟机安装hadoop完全分布式集群
- Activity监听返回按键
- IntelIj 使用
- ctags+taglist+cscope
- 动态顺序表的增、删、查、改、逆置、排序
- 私有云工具
- grunt live reload 配置记录
- toolbox整理
- ROS下Kinect2的驱动安装及简单应用
- ajax
- JavaScript 禁止Backspace 键
- leetcode 217. Contains Duplicate
- YUV图像实时去雾算法的优化与改进