instanceof

instanceof 运算符简介

在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "object"。ECMAScript 引入了另一个 Java 运算符 instanceof 来解决这个问题。instanceof 运算符与 typeof 运算符相似,用于识别正在处理的对象的类型。与 typeof 方法不同的是,instanceof 方法要求开发者明确地确认对象为某特定类型。例如:

清单 1. instanceof 示例
 var oStringObject = new String("hello world"); 
 console.log(oStringObject instanceof String); 	 // 输出 "true"

这段代码问的是“变量 oStringObject 是否为 String 对象的实例?”oStringObject 的确是 String 对象的实例,因此结果是"true"。尽管不像 typeof 方法那样灵活,但是在 typeof 方法返回 "object" 的情况下,instanceof 方法还是很有用的。

instanceof 运算符的常规用法

通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型。例如:

清单 2. instanceof 常规用法
 // 判断 foo 是否是 Foo 类的实例
 function Foo(){} 
 var foo = new Foo(); 
 console.log(foo instanceof Foo)//true

另外,更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。例如:

清单 3. instanceof 在继承中关系中的用法
 // 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例
 function Aoo(){} 
 function Foo(){} 
 Foo.prototype = new Aoo();//JavaScript 原型继承

 var foo = new Foo(); 
 console.log(foo instanceof Foo)//true 
 console.log(foo instanceof Aoo)//true
console.log(Foo instanceof Aoo)//false,因为Foo不是实例
console.log(Foo instanceof Foo)//false

上面的代码中是判断了一层继承关系中的父类,在多层继承关系中,instanceof 运算符同样适用。

你真的了解 instanceof 操作符吗?

看了上面的代码示例,是不是觉得 instanceof 操作符很简单,下面来看点复杂的用法。

清单 4. instanceof 复杂用法
 console.log(Object instanceof Object);//true 
 console.log(Function instanceof Function);//true 
 console.log(Number instanceof Number);//false 
 console.log(String instanceof String);//false 

 console.log(Function instanceof Object);//true 

 console.log(Foo instanceof Function);//true 
 console.log(Foo instanceof Foo);//false

看了上面的代码是不是又晕头转向了?为什么 Object 和 Function instanceof 自己等于 true,而其他类 instanceof 自己却又不等于 true 呢?如何解释?要想从根本上了解 instanceof 的奥秘,需要从两个方面着手:1,语言规范中是如何定义这个运算符的。2,JavaScript 原型继承机制。

01ECMASCRIPT 5.1 Standard文档中的定义:
0211.8.6 The instanceof operator
03 
04The production RelationalExpression : RelationalExpression instanceof ShiftExpression is evaluated as follows:
05 
061.Let lref be the result of evaluating RelationalExpression.
072.Let lval be GetValue(lref).
083.Let rref be the result of evaluating ShiftExpression.
094.Let rval be GetValue(rref).
105.If Type(rval) is not Object, throw a TypeError exception.
116.If rval does not have a [[HasInstance]] internal method, throw a TypeError exception.
127.Return the result of calling the [[HasInstance]] internal method of rval with argument lval.
13 
1415.3.5.3 [[HasInstance]] (V)
15 
16Assume F is a Function object.
17 
18When the [[HasInstance]] internal method of F is called with value V, the following steps are taken:
19 
20//V不是对象,直接返回false
211.If V is not an object, return false.
22 
23//用 [[Get]] 方法取 F方法的prototype属性,结果赋值给O
242.Let O be the result of calling the [[Get]] internal method of F with property name "prototype".
25 
26//如果O不是一个对象,报告异常
273.If Type(O) is not Object, throw a TypeError exception.
28 
29//重复循环
304.Repeat
31    //V = V.[[Prototype]]
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.
35 
36NOTE Function objects created using Function.prototype.bind have a different implementation of [[HasInstance]] defined in 15.3.4.5.3.

对应上述规范做个函数模拟A instanceof B:

01function _instanceof(A, B) {
02    var O = B.prototype;// 取B的显示原型
03    A = A.__proto__;// 取A的隐式原型
04    while (true) { 
05        //Object.prototype.__proto__ === null
06        if (A === null
07            return false
08        if (O === A)// 这里重点:当 O 严格等于 A 时,返回 true 
09            return true
10        A = A.__proto__;
11    }
12}

使用此函数模拟解析过程:

01Object instanceof Object解析,执行_instanceof (Object, Object)
02O = Object.prototype;
03A = Object.__proto__ = Function.prototype
04A = Function.prototype.__proto__ = Object.prototype
05return true;
06 
07Function instanceof Function解析,执行_instanceof (Function, Function)
08O = Function.prototype;
09A = Function.__proto__ = Function.prototype;
10return true;
11 
12Function instanceof Object解析,执行_instanceof (Function, Object)
13O = Object.prototype;
14A = Function.__proto__ = Function.prototype;
15A = Function.prototype.__proto__ = Object.prototype;
16return true;
17 
18String instanceof String解析,执行_instanceof (String, String)
19O = String.prototype;
20A = String.__proto__ = Function.prototype;
21A = Function.prototype.__proto__ = Object.prototype;
22A = Object.prototype.__proto__ = null;
23return false;
24 
25Ben instanceof Ben解析,执行_instanceof (Ben, Ben)
26O = Ben.prototype;
27A = Ben.__proto__ = Ben.prototype;
28A = Ben.prototype.__proto__ = Object.prototype;
29A = Object.prototype.__proto__ = null;
30return false;

 

转载于:https://www.cnblogs.com/fireporsche/p/6560405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值