当前位置:首页 >> 网络编程

js 判断各种数据类型的简单方法(推荐)

了解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   undefinedobject和函数类型 function

看到这里你肯定会问了:我怎么去区分对象,数组和null呢"htmlcode">

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 判断各种数据类型的简单方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。