首页 > 代码库 > JS判断对象类型

JS判断对象类型

对于确定JS内置对象类型,JS提供了typeof运算符,该运算符得到的结果为以下6种:number,boolean,string,function,object,undefined.不过对绝大多数对象而言,typeof都返回"object",无法确定具体的类型。我们使用一种函数Object.prototype.toString.call来判断

<script>        var a = 1;        console.log("a:"+typeof a);    //number        console.log("a:"+Object.prototype.toString.call(a));    //[object Number]        var b = NaN;        console.log("b:"+typeof b);    //number        console.log("b:"+Object.prototype.toString.call(b));    //[object Number]        var c= Number.MIN_VALUE;        console.log("c:"+typeof c);    //number        console.log("c:"+Object.prototype.toString.call(c));    //[object Number]        var d= Infinity;        console.log("d:"+typeof d);    //number        console.log("d:"+Object.prototype.toString.call(d));    //[object Number]        var e= "pmx";        console.log("e:"+typeof e);    //string        console.log("e:"+Object.prototype.toString.call(e));    //[object String]                var f= true;        console.log("f:"+typeof f);    //boolean        console.log("f:"+Object.prototype.toString.call(f));    //[object Boolean]        var g= window;        console.log("g:"+typeof g);    //object        console.log("g:"+Object.prototype.toString.call(g));    //[object Window]        var h = [1];        console.log("h:"+typeof h);    //object        console.log("h:"+Object.prototype.toString.call(h));    //[object Array]        var i = function(){};        console.log("i:" + typeof i);    //function         console.log("i:"+Object.prototype.toString.call(i));    //[object Function]        var j = document;        console.log("j:" + typeof j);    //object        console.log("j:"+Object.prototype.toString.call(j));    //[object HTMLDocument]        var k = null;        console.log("k:" + typeof k);    //object        console.log("k:"+Object.prototype.toString.call(k));    //[object Null]        var l = undefined;        console.log("l:" + typeof l);    //undefined        console.log("l:"+Object.prototype.toString.call(l));    //[object Undefined]        var m = eval;        console.log("m:" + typeof m);    //function        console.log("m:"+Object.prototype.toString.call(m));    //[object Function]        var n = new Date();        console.log("n:" + typeof n);    //object        console.log("n:"+Object.prototype.toString.call(n));    //[object Date]  </script>

 将Object.prototype.toString.call的返回值处理下,使得更直观

<script>var class2type = {};  var types = "Boolean Number String Function Array Date RegExp Object Error".split(" ");  for(var i=0; i<types.length; i++){      class2type["[object "+types[i]+"]"] = types[i].toLowerCase();  }  function type(obj){      var type = Object.prototype.toString.call(obj);      return class2type[type];  }console.log(type([1]));//array</script>

 

JS中还有个类似的运算符instanceof,它是用来判断某个对象是否是某类型的实例

<script>        var a = function(){}        console.log(a instanceof Object);//true        var b = 1;        console.log(b instanceof Object);//false        var c = "1";        console.log(c instanceof Object);//false        var d = true;        console.log(d instanceof Object);//false        var e = undefined;        console.log(e instanceof Object);//false        var f = null;        console.log(f instanceof Object);//false        var g = [1];        console.log(g instanceof Object);//true        var h = window;        console.log(h instanceof Object);//gte IE 9:true,lt IE 9:false </script>

 

可以看到,JS中的5中基本类型number,string,boolean,undefined,null并不是Object的实例。

对于低版本的浏览器认为window对象是dom里面的属性,和JS对象是两码事

 

JS判断对象类型