js数据类型的判断方法
判断js中的数据类型有一下几种方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下来主要比较一下这几种方法的异同。
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";};
最常见常用的判断方法:typeof【typeof 变量】
alert(typeof a) ------------> string
alert(typeof b) ------------> number
alert(typeof c) ------------> object
alert(typeof d) ------------> object
alert(typeof e) ------------> function
alert(typeof f) ------------> function
其中typeof返回的类型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true
alert(typeof a == String) ---------------> false
另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。
判断已知对象类型的方法: instanceof【已知对象 instanceof 对象类型】大小写一点都不能错
alert(c instanceof Array) ---------------> true
alert(d instanceof Date)
alert(f instanceof Function) ------------> true
alert(f instanceof function) ------------> false
注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
根据对象的constructor判断: constructor【类继承时会出错】
alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
注意: constructor 在类继承时会出错
eg:
function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var aObj = new A();
alert(aobj.constructor === B) -----------> true;
alert(aobj.constructor === A) -----------> false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
alert(aobj instanceof B) ----------------> true;//判断类型是否相同
alert(aobj instanceof B) ----------------> true;
言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
aobj.constructor = A; //将自己的类赋值给对象的constructor属性
alert(aobj.constructor === A) -----------> true;
alert(aobj.constructor === B) -----------> false; //基类不会报true了;
通用但很繁琐的方法: prototype【大小写不能写错】
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;
大小写不能写错,比较麻烦,但胜在通用。
无敌万能的方法:jquery.type()
如果对象是undefined或null,则返回相应的“undefined”或“null”。
jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined"
jQuery.type( null ) === "null"
如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字。 (有关此技术的更多细节。 )
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.9
jQuery.type( /test/ ) === "regexp"
其他一切都将返回它的类型“object”。
通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法。
参考链接:http://blog.sina.com.cn/s/blog_51048da70101grz6.html
js数据类型的判断方法的更多相关文章
- 前端基础——js数据类型及判断方法
一.数据类型 我们通常熟知的数据类型有六种,包括5种基本数据类型(Number, String, Boolean, Undefined, Null)和一种引用数据类型(Object).ES6又新增了一 ...
- js中的数据类型及判断方法
ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型. 基本类型 ● Boolean ● Null ● Undefined ● Number ● String ● Symbol (ECM ...
- 鉴别JS数据类型的全套方法
ECMAScript 标准定义了 7 种数据类型:Boolean.Null.Undefined.Number.String.Symbol(ES6新增)和Object,除Object以外的那6种数据类型 ...
- JS数据类型的判断
在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示: 基本类型:String.Number.Boolean.Symbol.Undefine ...
- js数据类型及判断数据类型
众所周知,js有7种数据类型 1. null 2. undefined 3. boolean 4. number 5. string 6. 引用类型(object.array.function) 7. ...
- js字符串常用判断方法
转自:http://blog.sina.com.cn/s/blog_6819fa800100j5t6.html 一.方法介绍 function obj$(id) ...
- js数据类型之判断
js有几种类型,具体是:字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未定义(Undefined). js提供了typ ...
- js 数据类型的判断
1. typeof 运算符 typeof 可以判断基本数据类型: typeof 123; // "number" typeof 'abc'; // "string&quo ...
- js的浏览器判断方法
使用navigator.userAgent来判断浏览器类型. 1.浏览器版本号函数: var br=navigator.userAgent.toLowerCase(); var browserVe ...
随机推荐
- Jsonp 原理简述
浏览器因为安全问题而有一个同源策略,不允许跨域请求数据.但是 js 脚本资源和图片资源还是被允许跨域请求的.在 web 开发中,遇到要跨域请求 json 数据时,可以用 jsonp 的方式来绕过浏览器 ...
- python学习交流 - 匿名函数
匿名函数 匿名函数是一个很有特色的定义函数的功能.在实际使用的过程,用户有时不得不为一些一行代码即可实现的功能来定义一个函数,例如像map, max, filter等内置函数的key参数只能接收可调用 ...
- ZOJ [P2314] 无源汇点有上下界模版
对于有上下界的网络流来说,我们可以分离出必要弧,然后将必要弧切开,两端分别连接源点和汇点,原图有可行解充要于源点或汇点满流. 这样求下来,只能求出可行流 #include <iostream&g ...
- BZOJ 3729: Gty的游戏 [伪ETT 博弈论]【学习笔记】
题意: 给定一棵有根树,每个节点有一些石子,每次可以将不多于k的石子移动到父节点 修改一个点的石子数,插入一个点,询问某棵子树是否先手必胜 显然是一个阶梯Nim 每次最多取k个,找规律或者观察式子易发 ...
- POJ 2653 Pick-up sticks [线段相交 迷之暴力]
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12861 Accepted: 4847 D ...
- BZOJ 2631: tree [LCT splay区间]
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 3854 Solved: 1292[Submit][Status][Discus ...
- 从一个实例学习----FLASK-WTF
本案例通过实现一个注册页面的编写,来带你了解FLASK-WTF的运用. 主要功能为表单基础的功能--手机号码必须为11位数,且通过数据库查找不能有已经注册的了,密码要求输入两遍且必须一样,且所有内容不 ...
- 六、BeautifulSoup4------自动登录网站(手动版)
每天一个小实例:(按照教学视频上自动登录的网站,很容易就成功了.自已练习登录别的网站,问题不断) 这个自己分析登录boss直聘.我用了一下午的时间,而且还是手动输入验证码,自动识别输入验证码的还没成功 ...
- iOS 使用NTP时间同步服务
githup上有相关开源库, ios-ntp 导入即可使用 NetworkClock *netClock = [NetworkClock sharedNetworkClock]; netClock.n ...
- 解决windows10和ubuntu16.04双系统下时间不对的问题
电脑安装完windows10与ubuntu16.04双系统后,Ubuntu的时间总会和Windows的时间相差8小时,原因在于windows认为BIOS时间是本地时间,Ubuntu认为BIOS时间是U ...