instanceof
2016-04-22 12:59
148 查看
The
Syntax
Description
The
Note that if the value of an
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,
Note for Mozilla developers:
In code using XPCOM
Examples
Demonstrating that
The following code uses
However, objects created with the object literal notation are an exception here: Although the prototype is undefined,
Demonstrating that
The following code creates an object type
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 instanceofxpcomInterfaceworks 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
相关文章推荐
- 设计模式开篇(一)
- EventBus使用
- Android Logcat小结和adb常用命令
- [转]escape()、encodeURI()、encodeURIComponent()区别详解
- OpenCV读取图片并显示和保存图片
- 剑指offer面试题 二进制中1的个数
- 【数据结构】二叉树(前、中、后)序遍历的递归与非递归算法
- 一次GPRS调试引出的大发现--STM32发送字符串第一个字符丢失
- Android Studio常用快捷键
- H5在线编辑器优化总结
- OpenCV在Windows、Linux、Android、iOS上的安装
- log4j配置 logging.xml (转载)
- 如何给教授发邮件
- Sqoop 数据导入问题
- C++注释规范
- 发布网站配置IIS(把网上找到的解决方法综合了一下)
- Feuding Families and Former Friends: Unsupervised Learning for Dynamic Fictional Relationships-Naacl 2016-20160422
- java.lang.NumberFormatException: For input string:"filesId"
- 最长回文子串(转载自网易博客:鼻子很帅的猪)
- 关于NodeJS的Cookie和Session机制