对于确定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的返回值处理下,使得更直观

var Adsame =function(){

        };

        Adsame.prototype.whichType = function(){
            var class2type = {};
            var types = "Boolean Number String Function Array Date RegExp Object Error Undefined Null";
            types = types.split(" ");
            for(var i = 0; i<types.length; i++){
                class2type["[object "+types[i]+"]"] = types[i].toLowerCase();
            }

            var params = arguments;
            var params_len = params.length;
            var param_arr = Array.prototype.slice.call(params);

            var result = [] ;
            for(var k = 0; k<params_len; k++){
                var type = Object.prototype.toString.call(param_arr[k]);
                result.push(class2type[type]);
            }
            return result.length ? result: "undefined" ;
        }

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判断对象类型的更多相关文章

  1. js 判断对象类型

    在企业级的开发中,我们常用 typeof 来判断企业 对象类型:但是 typeof 不能判断 Array 和 null 这里我们使用一个 原型上的 toString方法:请看一下代码: <scr ...

  2. html5 -js判断undefined类型

    js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...

  3. js判断浏览器类型 js判断ie6不执行

    js判断浏览器类型 $.browser  对象 $.browser.version 浏览器版本 var binfo = ''; if ($.browser.msie) { binfo = " ...

  4. js判断undefined类型,undefined,null,NaN的区别

    js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined   所以自作聪明判断       ...

  5. js验证对象类型

    js验证对象类型 1. Object.prototype.toString.call() 这是最佳解决方案,可以用作通用方式处理.各种类型的判断依据类似于[object Object],替换的是后边的 ...

  6. js判断undefined类型,undefined,null, 的区别详细解析

    js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...

  7. js判断undefined类型

    js判断undefined类型 if (reValue== undefined){    alert("undefined");    }  发现判断不出来,最后查了下资料要用ty ...

  8. Js判断对象是否为空,Js判断字符串是否为空

    Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...

  9. 通过JS判断联网类型和连接状态

    通过JS判断联网类型和连接状态 中国的移动网络环境复杂,为了给用户带去更好访问体验,开发者希望能了解用户当前的联网方式,然后给用户一个符合当前网络环境的请求结果. W3C的规范中给出了一个方法来获得现 ...

随机推荐

  1. 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法

    首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...

  2. C#泛类型链表的实现

    使用泛型LinkedList<T>类.下面的方法创建了一个LinkedList<T>类,并往链表对象中添加节点,然后使用了几种方法从链表节点中获得信息.       publi ...

  3. Channel 笔记本项目 (门户客户端 和 wp7客户端(介绍1))

    Channel 笔记本项目:(所包含 门户客户端 和 wp7客户端)              首先wp7客户端中,首页向右滑行,到了新闻(博文):(点触某篇新闻后,进入到新闻详细页面,在菜单栏所对应 ...

  4. JAVA计算文件大小

    File f = new File(save_path+File.separator + resouce_id+".zip"); FileInputStream fis = new ...

  5. Codeforces Round #208 (Div. 2) B Dima and Text Messages

    #include <iostream> #include <algorithm> #include <string> using namespace std; in ...

  6. topcoder SRM 591 DIV2 TheArithmeticProgression

    #include <iostream> #include <cstdlib> using namespace std; class TheArithmeticProgressi ...

  7. 【wikioi】1217 借教室

    题目链接http://www.wikioi.com/problem/1217/ 算法:二分答案(线段树可过wikioi数据) 二分:http://www.wikioi.com/solution/lis ...

  8. JS倒计时代码

    第一种:精确到秒的javascript倒计时代码 HTML代码: <form name="form1"> <div align="center" ...

  9. C# 从数据库中删除,插入,修改 索引选中条目

    一.删除 1. ) { var currentSelectIndex = usrListView.SelectedIndex; var item = usrView[currentSelectInde ...

  10. Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50

    Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50 分类: 系统运维 查找包含BOM头的文件,命令如下: 点击(此处)折叠或打开 grep -r -I -l ...