instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上。
实例一:普遍用法

A instanceof B :检测B.prototype是否存在于参数A的原型链上.

1 function Ben() {
2  
3 }
4 var ben = new Ben();
5 console.log(ben instanceof Ben);//true

 实例二:继承中判断实例是否属于它的父类

1 function Ben_parent() {}
2 function Ben_son() {}
3 Ben_son.prototype = new Ben_parent();//原型继承
4 var ben_son = new Ben_son();
5 console.log(ben_son instanceof Ben_son);//true
6 console.log(ben_son instanceof Ben_parent);//true

 实例三:复杂用法

1 function Ben() {}
2 console.log(Object instanceof Object);     //true
3 console.log(Function instanceof Function); //true
4 console.log(Function instanceof Object);   //true
5 console.log(Ben instanceof Function);      //true
6  
7 console.log(String instanceof String);   //false
8 console.log(Boolean instanceof Boolean); //false
9 console.log(Ben instanceof Ben);         //false

看到上述的结果,是否有点懵了,究其原因,还需探其原理,下面我们来看看规范中如何定义的。

01 ECMASCRIPT 5.1 Standard文档中的定义:
02 11.8.6 The instanceof operator
03  
04 The production RelationalExpression : RelationalExpression instanceof ShiftExpression is evaluated as follows:
05  
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.
13  
14 15.3.5.3 [[HasInstance]] (V)
15  
16 Assume F is a Function object.
17  
18 When the [[HasInstance]] internal method of F is called with value V, the following steps are taken:
19  
20 //V不是对象,直接返回false
21 1.If V is not an object, return false.
22  
23 //用 [[Get]] 方法取 F方法的prototype属性,结果赋值给O
24 2.Let O be the result of calling the [[Get]] internal method of F with property name "prototype".
25  
26 //如果O不是一个对象,报告异常
27 3.If Type(O) is not Object, throw a TypeError exception.
28  
29 //重复循环
30 4.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  
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) {
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 }

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

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

 参考链接:

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)的更多相关文章

  1. JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  2. JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  3. JS面向对象(2) -- this的使用,对象之间的赋值,for...in语句,delete使用,成员方法,json对象的使用,prototype的使用,原型继承与原型链

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  4. node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法

    1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...

  5. JS 学习(四)对象

    对象 在JS中,对象是数据(变量),拥有属性和方法. JS中所有事物都是对象:字符串.数字.数组.日期等. 对象是拥有属性和方法的特殊数据类型. 属性是与对象相关的值. 方法是能够在对象上执行的动作. ...

  6. node.js学习(1)

    新建便笺 3 node.js学习(1) 1)安装 http://nodejs.org/download/下载. 2)编写一个案例 var http=require("http"); ...

  7. 微信公众平台Js API(WeixinApi)

    微信公众平台Js API(WeixinApi): https://github.com/zxlie/WeixinApi#user-content-3%E9%9A%90%E8%97%8F%E5%BA%9 ...

  8. 【高德地图API】从零开始学高德JS API(七)——定位方式大揭秘

    原文:[高德地图API]从零开始学高德JS API(七)——定位方式大揭秘 摘要:关于定位,分为GPS定位和网络定位2种.GPS定位,精度较高,可达到10米,但室内不可用,且超级费电.网络定位,分为w ...

  9. 【高德地图API】从零开始学高德JS API(八)——地址解析与逆地址解析

    原文:[高德地图API]从零开始学高德JS API(八)——地址解析与逆地址解析 摘要:无论是百度LBS开放平台,还是高德LBS开放平台,其调用量最高的接口,必然是定位,其次就是地址解析了,又称为地理 ...

随机推荐

  1. Kubernetes dashboard集成heapster

    图形化展示度量指标的实现需要集成k8s的另外一个Addons组件: Heapster . Heapster原生支持K8s(v1.0.6及以后版本)和 CoreOS ,并且支持多种存储后端,比如: In ...

  2. Android Http POST文件上传之-----RFC1867协议

    RFC1867协议介绍            RFC1867协议主要是在HTTP协议的基础上为INPUT标签添加了file属性.同一时候限定了Form的method必须为POST,ENCTYPE必须为 ...

  3. Socket网络通讯开发总结之:Java 与 C进行Socket通讯(转)

    先交待一下业务应用背景:服务端:移动交费系统:基于C语言的Unix系统客户端:增值服务系统:基于Java的软件系统通迅协议:采用TCP/IP协议,使用TCP以异步方式接入数据传输:基于Socket流的 ...

  4. IReferenceCounted DotNetty.Common

    // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file ...

  5. 淘宝分布式配置管理服务Diamond

    转载:http://blog.csdn.net/kevinlynx/article/details/40017109 在一个分布式环境中,同类型的服务往往会部署很多实例.这些实例使用了一些配置,为了更 ...

  6. MyEclipse图表工具Birt的使用技巧(三)--连接webservice数据源

    Web Services 技术是一套标准.它定义了应用程序怎样在Web上实现互操作. 用户能够使用不论什么语言.在不同的平台下编写Web Services.然后通过Web Services 的标准来对 ...

  7. Python——网络编程,如何避免死锁?

    问题描述:什么是死锁? 死锁发生在当一个服务器和客户端同时试图往一个连接上写东西或同时从一个连接上读的时候.在这种情况下,没有进程可以得到任何数据(如果它们都正在读),因此,如果它们正在写,向外的bu ...

  8. 用shell查找某目录下的最大文件(转)

    这是一个很有趣的问题,因为作为一个shell菜鸟,我第一时间是没有任何想法的.心里纳闷为什么这样的操作Linux居然没有直接的命令实现这样的查询. 很自然地,第一感觉就是用awk去实现,因为菜鸟我看a ...

  9. android js 互相调用

    代码地址如下:http://www.demodashi.com/demo/13107.html android js 互相调用 第二版 支持js匿名函数接收 支持js json对象接收 支持js函数返 ...

  10. pandas 绘图与滑窗

    #import nessary library before start import pandas as pd import numpy as np import matplotlib.pyplot ...