js 判断数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下来主要比较一下这几种方法的异同。
先举几个例子:
|
1
2
3
4
5
6
|
var a = "iamstring.";var b = 222;var c= [1,2,3];var d = new Date();var e = function(){alert(111);};var f = function(){this.name="22";}; |
1、最常见的判断方法:typeof
|
1
2
3
4
5
6
|
alert(typeof a) ------------> stringalert(typeof b) ------------> numberalert(typeof c) ------------> objectalert(typeof d) ------------> objectalert(typeof e) ------------> functionalert(typeof f) ------------> function |
其中typeof返回的类型都是字符串形式,需注意,例如:
|
1
2
|
alert(typeof a == "string") -------------> truealert(typeof a == String) ---------------> false |
另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。
2、判断已知对象类型的方法: instanceof
|
1
2
3
4
|
alert(c instanceof Array) ---------------> truealert(d instanceof Date)alert(f instanceof Function) ------------> truealert(f instanceof function) ------------> false |
注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
3、根据对象的constructor判断: constructor
alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
注意: constructor 在类继承时会出错
eg:
|
1
2
3
4
5
6
|
function A(){};function B(){};A.prototype = new B(); //A继承自Bvar aObj = new A();alert(aobj.constructor === B) -----------> true;alert(aobj.constructor === A) -----------> false; |
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
|
1
2
|
alert(aobj instanceof B) ----------------> true;alert(aobj instanceof B) ----------------> true; |
言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
|
1
2
3
|
aobj.constructor = A; //将自己的类赋值给对象的constructor属性alert(aobj.constructor === A) -----------> true;alert(aobj.constructor === B) -----------> false; //基类不会报true了; |
4、通用但很繁琐的方法: prototype
|
1
2
3
4
5
6
|
alert(Object.prototype.toString.call(a) === ‘[object String]') -------> true;alert(Object.prototype.toString.call(b) === ‘[object Number]') -------> true;alert(Object.prototype.toString.call(c) === ‘[object Array]') -------> true;alert(Object.prototype.toString.call(d) === ‘[object Date]') -------> true;alert(Object.prototype.toString.call(e) === ‘[object Function]') -------> true;alert(Object.prototype.toString.call(f) === ‘[object Function]') -------> true; |
大小写不能写错,比较麻烦,但胜在通用。
5、无敌万能的方法:jquery.type()
如果对象是undefined或null,则返回相应的“undefined”或“null”。
|
1
2
3
4
|
jQuery.type( undefined ) === "undefined"jQuery.type() === "undefined"jQuery.type( window.notDefined ) === "undefined"jQuery.type( null ) === "null" |
如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字。 (有关此技术的更多细节。 )
|
1
2
3
4
5
6
7
8
|
jQuery.type( true ) === "boolean"jQuery.type( 3 ) === "number"jQuery.type( "test" ) === "string"jQuery.type( function(){} ) === "function"jQuery.type( [] ) === "array"jQuery.type( new Date() ) === "date"jQuery.type( new Error() ) === "error" // as of jQuery 1.9jQuery.type( /test/ ) === "regexp" |
其他一切都将返回它的类型“object”。
通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法
js 判断数据类型的几种方法的更多相关文章
- JS 判断数据类型的三种方法
说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...
- js判断数据类型的四种方法
1.typeof typeof是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型.返回的结果用该类型的字符串(全小写字母)形式表示,包括number,string,boolean,und ...
- [转]js判断数据类型的四种方法
原文地址:https://www.cnblogs.com/crackedlove/p/10331317.html 1.typeof typeof是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的 ...
- js中判断数据类型的四种方法总结
js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...
- javascript 判断数据类型的几种方法
javascript 判断数据类型的几种方法一.typeof 直接返回数据类型字段,但是无法判断数组.null.对象 typeof 1 "number" typeof NaN &q ...
- JS中判断数据类型的几种方法
1⃣️首先我们来了解一下js中的数据类型 1.基本数据类型:Undefined.Null.Boolean.Number.String(值类型) 2.复杂数据类型:Object(引用类型) (值类型和引 ...
- js判断类型的四种方法
typeof:使用typeof可以很方便的判断六种类型:undefined.boolean.string.number.object.function 数组和null会被判断为object类型 ins ...
- 判断数组的方法/判断JS数据类型的四种方法
参考文: 以下 3 个判断数组的方法,请分别介绍它们之间的区别和优劣Object.prototype.toString.call() . instanceof 以及 Array.isArray() h ...
- js中判断数据类型的4中方法
注意: js中数据类型有7种(number, boolean, string, null, undefined, object, Symbol(es6新增)) 原始数据类型: number, stri ...
随机推荐
- cmder小技巧
1.修改config下的aliases文件,可以添加别名 $* 表示所有参数,$i 表示第几个参数 比如cd cd=cd /d $* 这样window下的cd就可以直接切换盘符+路径了. 有用的别名 ...
- java 关于同步异步的理解
经常看到介绍 ArrayList 和HashMap是异步,Vector和HashTable是同步,这里同步是线程安全的,异步不是线程安全的,举例说明: 当创建一个Vector对象时候, Vector ...
- 使用 LocalReport 对象进行打印
添加引用 从“项目”菜单中,选择“添加引用”.将显示“添加引用”对话框. 从“.NET”选项卡上显示的列表框中,选择 Winforms 和 Drawing 组件. 添加代码 应打开 Program ...
- Hadoop的体系结构之HDFS的体系结构
Hadoop的体系结构 Hadoop不仅是一个用于分布式存储的分布式文件系统,而是设计用来在由通用计算设备组成的大型集群上执行分布式应用的框架. HDFS和MapReduce是Hadoop中的两个最基 ...
- mysql初步调试
第一次这么近距离的接触源码,有点兴奋,又有点不知所措,尼玛随便一个源文件都有几千行,你说怎么看,还有一些数不清的数据结构,函数,类等乱七八糟的东西. 我装的mysql版本是5.7.12,然后运行平台是 ...
- C语言 · 提货单(P1101)
算法训练 P1101 时间限制:1.0s 内存限制:256.0MB 有一份提货单,其数据项目有:商品名(MC).单价(DJ).数量(SL).定义一个结构体prut,其成员是上面的三项 ...
- nginx的centos和rhel的yum配置安装
Official Red Hat/CentOS packages To add NGINX yum repository, create a file named /etc/yum.repos.d/n ...
- wireshark error: There are no interfaces on which a capture can be done.
一.Linux环境:1.root用户启动 01.启动一个shell 02.sudo wireshark (需要root权限) 2.普通用户启动 从Linux中第一次启动Wireshark的时候,可能会 ...
- FTP原理
1.1.1 ftp的主动模式和被动模式 扩展重要. FTP是仅基于TCP的服务,不支持UDP. 与众不同的是FTP使用2个端口,一个数据端口和一个命令端口(也可叫做控制端口).通常来说这两个端口是21 ...
- 用X264编码以后的H264数据
输入的数据准备好了,编码后的数据都在x264_nal_t的数组.我这里设置的参数是Baseline Profile,所以编码后没有B帧,将编码后的数据保存分析后发现,第一次编码的时候会有4个NAl,分 ...