js 判断各种数据类型 typeof 几种类型值
了解js的都知道, 有个typeof 用来判断各种数据类型,有两种写法:typeof xxx ,typeof(xxx)
如下实例:
typeof 2 输出 number
typeof null 输出 object
typeof {} 输出 object
typeof [] 输出 object
typeof (function(){}) 输出 function
typeof undefined 输出 undefined
typeof '222' 输出 string
typeof true 输出 boolean
这里面包含了js里面的五种数据类型 number string boolean undefined object和函数类型 function
看到这里你肯定会问了:我怎么去区分对象,数组和null呢?
接下来我们就用到另外一个利器:Object.prototype.toString.call
这是对象的一个原生原型扩展函数,用来更精确的区分数据类型。
我们来试试这个玩儿意儿:
var gettype=Object.prototype.toString
gettype.call('aaaa') 输出 [object String]
gettype.call(2222) 输出 [object Number]
gettype.call(true) 输出 [object Boolean]
gettype.call(undefined) 输出 [object Undefined]
gettype.call(null) 输出 [object Null]
gettype.call({}) 输出 [object Object]
gettype.call([]) 输出 [object Array]
gettype.call(function(){}) 输出 [object Function]
看到这里,刚才的问题我们解决了。 它还可以判断document , body 等等,继续看下面
jquery下面也有一个判断类型的如下:
- 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(/test/) === "regexp"
constructor也能判断数据类型:
如:''.constructor==String
[].constructor==Array
var obj= new Object() obj.constructor==Object
其实js 里面还有好多类型判断 [object HTMLDivElement] div 对象 , [object HTMLBodyElement] body 对象 ,[object Document](IE)或者 [object HTMLDocument](firefox,google) ......各种dom节点的判断,这些东西在我们写插件的时候都会用到。
可以封装的方法如下 :
var gettype=Object.prototype.toString
var utility={
isObj:function(o){
return gettype.call(o)=="[object Object]";
},
isArray:function(o){
return gettype.call(o)=="[object Array]";
},
isNULL:function(o){
return gettype.call(o)=="[object Null]";
},
isDocument:function(){
return gettype.call(o)=="[object Document]"|| [object HTMLDocument];
}
........
}
js 判断各种数据类型 typeof 几种类型值的更多相关文章
- js 判断各种数据类型
了解js的都知道, 有个typeof 用来判断各种数据类型,有两种写法:typeof xxx ,typeof(xxx) 如下实例: typeof 2 输出 number ...
- Js的typeof和Js的基本数据类型
本文将从以下几个方面介绍Js的typeof和Js的基本数据类型: ** Js的typeof的用法 ** Js的基本数据类型 ** 使用基本类型使用typeof的返回结果 ** Js的typeof的用法 ...
- 如何判断js中的数据类型?
js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...
- 如何判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- [转]如何判断js中的数据类型
原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...
- 如何判断js中的数据类型(转)
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- 判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- 判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- 转:判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
随机推荐
- MATLAB读取写入文本数据最佳方法 | Best Method for Loading & Saving Text Data Using MATLAB
MATLAB读取文件有很多方法.然而笔者在过去进行数据处理中,由于函数太多,相互混杂,与C#,Python等语言相比,反而认为读取文本数据比较麻烦.C#和Python等高级语言中,对于大部分的文本数据 ...
- 编写高质量代码改善C#程序的157个建议——建议3: 区别对待强制转型与as和is
建议3: 区别对待强制转型与as和is 在阐述本建议之前,首先需要明确什么是强制转型,以及强制转型意味着什么.从语法结构上来看,类似下面的代码就是强制转型. secondType = (SecondT ...
- Codeforces 900C. Remove Extra One(暴力)
You are given a permutation p of length n. Remove one element from permutation to make the number of ...
- Oracle 定时任务讲解
前几天,公司的job调度出现了问题,由于权限管的严,没有查看oracle 一些重要的数据字典,后面联系DBA,是由于数据库切换到备机时,参数设置不对,导致db job没有正常调度. 今天刚好有时间,想 ...
- Visual Studio 2013 新增功能:“Browser Link”
今天新装了 Visual Studio 2013, 使用 VS2013 打开一个现有的 WEB 网站, 在调试网站的时候出现在了脚本错误,一个文件名叫 "browserLink", ...
- c# 读取txt文档和写入文档的方法
StreamReader sr = new StreamReader(path); //path是要读取的文件的完整路径 String str_read = sr.ReadToEnd(); //从开始 ...
- 将.net core 发布到Linux上的一些坑
目前遇到的,之后遇到再加 1.时间格式要用.ToString("yyyy-MM-dd HH:mm:ss"); 2.文件路径要用Path.Combine(),IHostingEnvi ...
- Android SDK下载和更新慢或失败的解决办法
下载完Android SDK后发现无法更新,原因是我们被墙了,所以需要使用代理来更新,或者直接把dl-ssl.google.com解析的IP改一下就可以了 用文本编辑器打开文件C:\Windows\S ...
- 玩转php缓存memcache
记录 一.本地安装 链接:https://www.cnblogs.com/jkko123/p/6294669.html 二.linux安装
- asp手动给combox赋值
ASPxComboBox cbSex = ASPxGridView1.FindEditFormTemplateControl("cmbUSER_SEX") as ASPxCombo ...