javascript instanceof
object instanceof constructorinstanceof运算符用来检测constructor.prototype是否存在于参数object的原型链上. 对于instanceof和typeof,以前偶尔的用到过,特别是typeof用到的相对更多一些,今日研究ext源码,很多地方都用到了instanceof,突然觉得他们两个有些相似但也应该有他们区别,网上看了一些文章,对它们之间的关系有了一定的了解。 instanceof和typeof都能用来判断一个变量是否为空或是什么类型的变量。
typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined。我们可以使用typeof来获取一个变量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)因为如果a不存在(未声明)则会出错,对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。
(
var a=null;
alert(typeof a);
输出:Object)
如果我们希望获取一个对象是否是数组,或判断某个变量是否是某个对象的实例则要选择使用instanceof。instanceof用于判断一个变量是否某个对象的实例,如var a=new Array();alert(a instanceof Array);会返回true,同时alert(a instanceof Object)也会返回true;这是因为Array是object的子类。再如:function test(){};var a=new test();alert(a instanceof test)会返回true。
谈到instanceof我们要多插入一个问题,就是function的arguments,我们大家也许都认为arguments是一个Array,但如果使用instaceof去测试会发现arguments不是一个Array对象,尽管看起来很像。
https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Operators/instanceof
http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/#icomments
javascript instanceof的更多相关文章
- JavaScript instanceof 运算符深入剖析
简介: 随着 web 的发展,越来越多的产品功能都放在前端进行实现,增强用户体验.而前端开发的主要语言则是 JavaScript.学好 JavaScript 对开发前端应用已经越来越重要.在开发复杂产 ...
- JavaScript instanceof 运算符深入剖析【转载】
http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/ instanceof 运算符简介 在 JavaScript ...
- JavaScript instanceof 运算符
instanceof运算符简介 在 JavaScript 中 判断一个变量的类型常常会用 typeof 运算符 判断一个实例是否属于某种类型会使用instanceof 与 typeof 方法不同的是, ...
- javascript instanceof,typeof的区别
区分string 与 String的区别 为什么结果会是false呢? <script type="text/javascript"> var aColors = [& ...
- JavaScript instanceof深度剖析以及Object.prototype.toString.call()使用
本文由segementfalt上的一道instanceof题引出: var str = new String("hello world"); console.log(str ins ...
- JavaScript instanceof vs typeof
Use instanceof for custom typesvar ClassFirst = function () {};var ClassSecond = function () {};var ...
- JavaScript instanceof和typeof的区别
引用自: http://www.cnblogs.com/eoiioe/archive/2008/12/31/1366081.html instanceof和typeof都能用来判断一个变量是否为空 ...
- [JavaScript]instanceof String not behaving as expected in Google Apps Script
Link: http://stackoverflow.com/questions/11571923/instanceof-string-not-behaving-as-expected-in-goog ...
- JavaScript中typeof、toString、instanceof、constructor与in
JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...
随机推荐
- If-Modified-Since和If-None-Match
If-Modified-Since & If-None-MatchIf-Modified-Since,和 Last-Modified 一样都是用于记录页面最后修改时间的 HTTP 头信息,只是 ...
- GO学习资源站
GO语言学习资源网站 http://golangtc.com https://gobyexample.com http://golang-examples.tumblr.com
- 在WPF中自定义你的绘制(三)
原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三) ...
- (八)boost库之异常处理
(八)boost库之异常处理 当你面对上千万行的项目时,当看到系统输出了异常信息时,你是否想过,如果它能将文件名.行号等信息输出,该多好啊,曾经为此绞尽脑汁. 今天使用boost库,将轻松的解决这个问 ...
- 【LeetCode练习题】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- yum cannot retrieve metalink for repository
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
- windows2003 64位注册码 序列号 激活码
Windows 2003 R2 64bit Enterprise VOL Edition 企业版 MR78C-GF2CY-KC864-DTG74-VMT73 VPT7T-77D38-KWVW2-2G3 ...
- 把DEDE的在线文本编辑器换成Kindeditor不显示问题
在织梦论坛下载了[Kindeditor编辑器For DedeCMS],按照操作说明安装后,后台文章编辑的区域却显示空白,有人说不兼容V57版本,有人说不兼容gbk版本,我也纠结了很久,在网上找了很多版 ...
- GDB 多进程调试
启动: $gdb <file> || $gdb 然后(gdb)file <file> 运行: (gdb)run <该程序本身的命令行参数> 查看代码: (gdb) ...
- Hibernate问题之'hibernate.dialect' not set
继前文:Hibernate4中buildSessionFactory方法废弃问题.后 继续有问题.本来之前好好的项目,用了这种新的方法后发现问题. 出现 Connection cannot be n ...