js instanceof
Posted 蒂其之死
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js instanceof 相关的知识,希望对你有一定的参考价值。
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
http://www.zuojj.com/archives/393.html
以上是关于js instanceof 的主要内容,如果未能解决你的问题,请参考以下文章
js instanceof 解析
typeof & instanceof 原理
js中instanceof与typeof的区别以及instanceof的门道
js中的instanceof运算符
instanceof和typeof运算符的区别详解
js中的instanceof