js instanceof (2)
2024-09-03 05:56:32 217人阅读
instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上。
实例一:普遍用法
A instanceof B :检测B.prototype是否存在于参数A的原型链上.
5 | console.log(ben instanceof Ben); |
实例二:继承中判断实例是否属于它的父类
1 | function Ben_parent() {} |
3 | Ben_son.prototype = new Ben_parent(); |
4 | var ben_son = new Ben_son(); |
5 | console.log(ben_son instanceof Ben_son); |
6 | console.log(ben_son instanceof Ben_parent); |
实例三:复杂用法
2 | console.log(Object instanceof Object); |
3 | console.log(Function instanceof Function); |
4 | console.log(Function instanceof Object); |
5 | console.log(Ben instanceof Function); |
7 | console.log(String instanceof String); |
8 | console.log(Boolean instanceof Boolean); |
9 | console.log(Ben instanceof Ben); |
看到上述的结果,是否有点懵了,究其原因,还需探其原理,下面我们来看看规范中如何定义的。
01 | ECMASCRIPT 5.1 Standard文档中的定义: |
02 | 11.8.6 The instanceof operator |
04 | The production RelationalExpression : RelationalExpression instanceof ShiftExpression is evaluated as follows: |
06 | 1.Let lref be the result of evaluating RelationalExpression. |
07 | 2.Let lval be GetValue(lref). |
08 | 3.Let rref be the result of evaluating ShiftExpression. |
09 | 4.Let rval be GetValue(rref). |
10 | 5.If Type(rval) is not Object, throw a TypeError exception. |
11 | 6.If rval does not have a [[HasInstance]] internal method, throw a TypeError exception. |
12 | 7.Return the result of calling the [[HasInstance]] internal method of rval with argument lval. |
14 | 15.3.5.3 [[HasInstance]] (V) |
16 | Assume F is a Function object. |
18 | When the [[HasInstance]] internal method of F is called with value V, the following steps are taken: |
21 | 1.If V is not an object, return false. |
23 | //用 [[Get]] 方法取 F方法的prototype属性,结果赋值给O |
24 | 2.Let O be the result of calling the [[Get]] internal method of F with property name "prototype". |
27 | 3.If Type(O) is not Object, throw a TypeError exception. |
32 | a.Let V be the value of the [[Prototype]] internal property of V. |
33 | b.If V is null, return false. |
34 | c.If O and V refer to the same object, return true. |
36 | NOTE Function objects created using Function.prototype.bind have a different implementation of [[HasInstance]] defined in 15.3.4.5.3. |
对应上述规范做个函数模拟A instanceof B:
01 | function _instanceof(A, B) { |
使用此函数模拟解析过程:
01 | Object instanceof Object解析,执行_instanceof (Object, Object) |
03 | A = Object.__proto__ = Function.prototype |
04 | A = Function.prototype.__proto__ = Object.prototype |
07 | Function instanceof Function解析,执行_instanceof (Function, Function) |
08 | O = Function.prototype; |
09 | A = Function.__proto__ = Function.prototype; |
12 | Function instanceof Object解析,执行_instanceof (Function, Object) |
14 | A = Function.__proto__ = Function.prototype; |
15 | A = Function.prototype.__proto__ = Object.prototype; |
18 | String instanceof String解析,执行_instanceof (String, String) |
20 | A = String.__proto__ = Function.prototype; |
21 | A = Function.prototype.__proto__ = Object.prototype; |
22 | A = Object.prototype.__proto__ = null ; |
25 | Ben instanceof Ben解析,执行_instanceof (Ben, Ben) |
27 | A = Ben.__proto__ = Ben.prototype; |
28 | A = Ben.prototype.__proto__ = Object.prototype; |
29 | A = Object.prototype.__proto__ = null ; |
参考链接:
http://www.zuojj.com/tdocs/es5.1/#sec-11.8.6
http://blog.csdn.net/cuew1987/article/details/15498121
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/instanceof
转载声明:
本文标题:深入javascript(六):instanceof 运算符
本文链接:http://www.zuojj.com/archives/393.html,转载请注明转自Benjamin-专注前端开发和用户体验
http://www.zuojj.com/archives/393.html
js instanceof (2)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。