js instanceof (2)

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

时间: 2024-08-24 04:22:53

js instanceof (2)的相关文章

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

相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对象之间的赋值,for...in语句,delete使用,成员方法,json对象的使用,prototype的使用,原型继承与原型链 JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法 1.Object类 在JS中,Object是所有类的基

JS数组(Array)操作汇总

1.去掉重复的数组元素.2.获取一个数组中的重复项.3.求一个字符串的字节长度,一个英文字符占用一个字节,一个中文字符占用两个字节.4.判断一个字符串中出现次数最多的字符,统计这个次数.5.数组排序. 6.快排. 7.删除/添加数组项. 8.数组随机顺序输出. 9.数组求和.最大值. 10.判断是否为数组. 11.冒泡排序. 1.去掉重复的数组元素. Array.prototype.unique = function() { var ret = []; var o = {}; for(var i

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

摘要:关于定位,分为GPS定位和网络定位2种.GPS定位,精度较高,可达到10米,但室内不可用,且超级费电.网络定位,分为wifi定位和基站定位,都是通过获取wifi或者基站信息,然后查询对应的wifi或者基站位置数据库,得到的定位地点.定位数据库可以不断完善不断补充,所以,越定位越准确.本文详细描述了,如果使用高德JS API来实现位置定位.城市定位的方法,包含了IP定位,浏览器定位,检索定位等多种网络定位方法.当然,如果您的手机有GPS功能,那么使用浏览器定位的时候,会自动获取GPS信息,使

【高德地图API】从零开始学高德JS API(四)搜索服务

摘要:地图服务,大家能想到哪些?POI搜素,输入提示,地址解析,公交导航,驾车导航,步行导航,道路查询(交叉口),行政区划等等.如果说覆盖物Marker是地图的骨骼,那么服务,就是地图的气血.有个各种各样的地图服务,我们的地图应用才能变得有血有肉,活灵活现.第四篇拆成了几个要点,本篇主要讲搜索服务.包括周边搜索,关键词搜索,范围搜索,搜索提示(自动完成,输入提示),行政区域,交叉路口,检索自有数据(云图). demo:http://zhaoziang.com/amap/zero_4_1.html

【高德地图API】从零开始学高德JS API(二)地图控件与插件——测距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨

摘要:无论是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装的一系列更加便于开发者使用,减少开发者工作量的二级API接口.除了官方通用的鱼骨.鹰眼控件,还有大量官方开发的地图插件,类似谷歌的lib.当然本文还会介绍自定义插件的使用. ------------------------------------------------------------------------------------------------- 第一部分 控件 目前官方支持的控件包含:缩放控制条-地图

# JS笔记(1)

 JS理论: 1.JavaScript是一个客户端脚本 ------工作在客户端的浏览器完成:相对应的PHP.ASP.NET .JSP 是一个服务端脚本. 2.JS可以插入到HTML中的任意一个位置,不过HTML解析式从上往下解析的,所以放在上面可能会找不到控件. 3.JS的特点: 脚本编程语言 基于对象的语言 由事件驱动 跨平台.依赖于浏览器.与操作环境无关 4.JS的作用: 表单的验证(放在客户端验证比较好)----可以减轻服务端的压力,并且用户体验感更好 页面的动态效果 动态改变页面的内容

微信公众平台Js API(WeixinApi)

zxlie/WeixinApi 微信公众平台Js API(WeixinApi)微信公众平台Js API(WeixinApi),布布扣,bubuko.com

【高德地图API】从零开始学高德JS API(五)路线规划——驾车|公交|步行

先来看两个问题:路线规划与导航有什么区别?步行导航与驾车导航有什么区别? 回答: 1.路线规划,指的是为用户提供3条路线推荐.[高德]在提供路线规划的时候,会提供用户自定义路线规划功能,这是别家没有做到的.导航,指的是为驾车用户提示路口信息,向左向右,进入匝道等信息. 2.我们这里说的步行导航和驾车导航,严格的说,应该是路线规划.从A地到B地,如果是驾车,路线规划会将公路路网做为搜索数据:如果是步行,过街天桥.地下通道.人行道做为搜索数据. ---------------------------

【高德地图API】从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索

摘要: 地图服务,大家能想到哪些?POI搜素,输入提示,地址解析,公交导航,驾车导航,步行导航,道路查询(交叉口),行政区划等等.如果说覆盖物Marker是地图的骨骼,那么服务,就是地图的气血.有个各种各样的地图服务,我们的地图应用才能变得有血有肉,活灵活现. 第四篇拆成了几个要点,本篇主要讲搜索服务.包括周边搜索,关键词搜索,范围搜索,搜索提示(自动完成,输入提示),行政区域,交叉路口,检索自有数据(云图). demo:http://zhaoziang.com/amap/zero_4_1.ht