instanceof
来源:互联网 发布:算法基本特征 编辑:程序博客网 时间:2024/05/16 12:05
The instanceof
operator tests whether an object has in its prototype chain the prototype
property of a constructor.
Syntax
object instanceof constructor
Parameters
object
- The object to test.
constructor
- Function to test against
Description
The instanceof
operator tests presence of constructor.prototype
in object
's prototype chain.
// defining constructorsfunction C(){}function D(){}var o = new C();// true, because: Object.getPrototypeOf(o) === C.prototypeo instanceof C;// false, because D.prototype is nowhere in o's prototype chaino instanceof D;o instanceof Object; // true, because:C.prototype instanceof Object // trueC.prototype = {};var o2 = new C();o2 instanceof C; // true// false, because C.prototype is nowhere in// o's prototype chain anymoreo instanceof C; D.prototype = new C(); // use inheritancevar o3 = new D();o3 instanceof D; // trueo3 instanceof C; // true
Note that if the value of an instanceof
test can change based on changes to the prototype
property 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].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 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)
Examples
Demonstrating that 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 typeObject
(they are derived from Object
).
However, objects created with the object literal notation are an exception here: Although the prototype is undefined, instanceof Object
returns 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 undefinedmyString instanceof String; // returns truenewStr instanceof String; // returns truemyString instanceof Object; // returns truemyObj instanceof Object; // returns true, despite an undefined prototype({}) instanceof Object; // returns true, same case as abovemyString instanceof Date; // returns falsemyDate instanceof Date; // returns truemyDate instanceof Object; // returns truemyDate instanceof String; // returns false
Demonstrating that mycar
is of type Car
and type Object
The following code creates an object type Car
and an instance of that object type, mycar
. Theinstanceof
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;}var mycar = new Car("Honda", "Accord", 1998);var a = mycar instanceof Car; // returns truevar b = mycar instanceof Object; // returns true
Specifications
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.StandardInitial definition. Implemented in JavaScript 1.4.
Browser compatibility
- Desktop
- Mobile
See also
typeof
- instanceof?
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- instanceof
- 2016.6.13 通过虚拟机安装hadoop完全分布式集群
- IOS相机字体英文转中文
- shell脚本 实现在git下两个提交下文件差异并打包
- 第9章 有没有替代原因
- 【leetcode】110. Balanced Binary Tree
- instanceof
- Java编程思想21.2.1实例
- CSP考试 2016年04月第1题 折点计数 C语言实现
- redis面试题总结
- iOS开发-Could not find a storyboard named 'Main' in bundle NSBundle
- 网页调用Activex实现,ocx封装成cab文件
- VS中VC++目录中的$是什么意思
- Hibernate Search-----关联检索
- 通向成熟之路