了解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 几种类型值的更多相关文章

  1. js 判断各种数据类型

    了解js的都知道, 有个typeof  用来判断各种数据类型,有两种写法:typeof   xxx   ,typeof(xxx) 如下实例: typeof   2      输出   number   ...

  2. Js的typeof和Js的基本数据类型

    本文将从以下几个方面介绍Js的typeof和Js的基本数据类型: ** Js的typeof的用法 ** Js的基本数据类型 ** 使用基本类型使用typeof的返回结果 ** Js的typeof的用法 ...

  3. 如何判断js中的数据类型?

    js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...

  4. 如何判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  5. [转]如何判断js中的数据类型

    原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...

  6. 如何判断js中的数据类型(转)

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  7. 判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  8. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  9. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

随机推荐

  1. eclipse导入tomcat

    官网下载tomcat压缩包 官网:http://tomcat.apache.org/ tomcat8:链接:http://pan.baidu.com/s/1kVHAEoR 密码:kyt8 tomcat ...

  2. php复制目录

    function copyDir($dirSrc,$dirTo) { if(is_file($dirTo)) { echo '目标不是目录不能创建!'; return; } if(!file_exis ...

  3. 【大数据之数据仓库】GreenPlum优化器对比测试

    在< [大数据之数据仓库]选型流水记>一文中有提及,当时没有测试GreenPlum的quicklz压缩算法和ORCA查询优化器,考虑到quicklz压缩算法因为版权问题不会开源(详情请参阅 ...

  4. JAVA学习必须掌握的框架,不看后悔

    Web应用,最常见的研发语言是Java和PHP. 后端服务,最常见的研发语言是Java和C/C++. 大数据,最常见的研发语言是Java和Python. 可以说,Java是现阶段中国互联网公司中,覆盖 ...

  5. linux下Tomcat+OpenSSL配置单向&双向认证(自制证书)

    背景 由于ios将在2017年1月1日起强制实施ATS安全策略,所有通讯必须使用https传输,本文只针对自制证书,但目前尚不确定自制证书是否能通过appstore审核. 1.必须支持传输层安全(TL ...

  6. ulimit -n 查看可以打开的最大文件描述符的数量

    具体ulimit命令参考 https://www.cnblogs.com/wangkangluo1/archive/2012/06/06/2537677.html

  7. SDUT OJ 顺序表应用4:元素位置互换之逆置算法

    顺序表应用4:元素位置互换之逆置算法 Time Limit: 10 ms Memory Limit: 570 KiB Submit Statistic Discuss Problem Descript ...

  8. BDD 相关整理---介绍

    # BDD介绍 ### 什么是BDD Behavior-driven development In software engineering, behavior-driven development ...

  9. ios网络 -- HTTP请求 and 文件下载/断点下载

    一:请求 http://www.jianshu.com/p/8a90aa6bad6b 二:下载 iOS网络--『文件下载.断点下载』的实现(一):NSURLConnection http://www. ...

  10. win7/win8/win10 系统

    WIN7/WIN8/WIN10 系统安装 http://www.windows7en.com/Win7/18572.html