了解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. JVM调用类的main方法的过程

    jvm先装载类,再链接类,再初始化类(以代码的文本顺序执行类变量初始化器.类静态初始化方法或接口的属性(field)初始化器),完成类的初始化后,才执行类的main方法.在链接过程中,可以静态链接(解 ...

  2. Ubuntu在用root账户使用xftp连接时提示拒绝连接

    一般来说Linux不允许使用root账户连接,修改配置 vi /etc/ssh/sshd_config #Authentication: LoginGraceTime PermitRootLogin ...

  3. c++实验2 顺序存储线性表

    线性表顺序存储 实现了动态数组的增删改查  前驱后继  A=AUB 动态数组右移 (1)顺序表存储结构的定义(类的声明): class SeqList { protected: DataType *l ...

  4. javascript选项卡2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. java大数据批量处理实现方式

    1. 各批量方式对比 Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 2. 原理解析 1)MySql PreparedStatement executeBatch过慢问题 3. 工 ...

  6. 1. 时间复杂度(大O表示法)以及使用python实现栈

    1.时间复杂度(大O表示法): O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n) < O(n! ...

  7. eclipse中查看java源码时,出现source not found问题

  8. minizip -基于zlib开源代码库

    转载:https://www.topomel.com/archives/979.html 一.minizip 是一套用来压缩和解压文件的工具,其基于zlib开源代码库. 开源代码下载链接:http:/ ...

  9. Qt 学习之路 2(41):model/view 架构

    Qt 学习之路 2(41):model/view 架构 豆子 2013年1月23日 Qt 学习之路 2 50条评论 有时,我们的系统需要显示大量数据,比如从数据库中读取数据,以自己的方式显示在自己的应 ...

  10. payload指安全测试数据

    payload,翻译过来是有效载荷 通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,比如数据量的大小.校验位等,这样就相当于给已经分批的原始 ...