1、通过 Object.prototype.toString.call() 进行类型判断

function isArray(obj) {

return Object.prototype.toString.call(obj) === '[object Array]';    
}

我们知道,Javascript中,一切皆为对象。所以如下代码,应当会输出对应字符
 
Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call('') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(Math) // "[object Math]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"
利用这个特性,可以写出一个比typeof运算符更准确的类型判断函数。
var type = function (o){
var s = Object.prototype.toString.call(o);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
}; type({}); // "object"
type([]); // "array"
type(5); // "number"
type(null); // "null"
type(); // "undefined"
type(/abcd/); // "regex"
type(new Date()); // "date"

在上面这个type函数的基础上,还可以加上专门判断某种类型数据的方法。
['Null',
'Undefined',
'Object',
'Array',
'String',
'Number',
'Boolean',
'Function',
'RegExp',
'NaN',
'Infinite'
].forEach(function (t) {
type['is' + t] = function (o) {
return type(o) === t.toLowerCase();
};
}); type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true

标准浏览器中完美的作到,但是(为什么要说但是呢)IE6中,却会出现以下问题:

通过Object.prototype.toString.call获取的 字符串,undefined,null均为Object
所以,我们又要悲剧的先对以上类型进行判断,完整代码:
var oP = Object.prototype,
toString = oP.toString;
 
function typeOf(value) {
    if (null === value) {
        return 'null';
    }
 
    var type = typeof value;
    if ('undefined' === type || 'string' === type) {
        return type;
    }
 
    var typeString = toString.call(value);
    switch (typeString) {
    case '[object Array]':
        return 'array';
    case '[object Date]':
        return 'date';
    case '[object Boolean]':
        return 'boolean';
    case '[object Number]':
        return 'number';
    case '[object Function]':
        return 'function';
    case '[object RegExp]':
        return 'regexp';
    case '[object Object]':
        if (undefined !== value.nodeType) {
            if (3 == value.nodeType) {
                return (/\S/).test(value.nodeValue) ? 'textnode': 'whitespace';
            } else {
                return 'element';
            }
        } else {
            return 'object';
        }
    default:
        return 'unknow';
    }
}

js的深入学习课程Object.prototype.toString.call()的更多相关文章

  1. 使用Object.prototype.toString.call()方法精确判断对象的类型

    在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...

  2. 浅谈Object.prototype.toString.call()方法

    在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object.对于null.array.function.o ...

  3. js 中调用 Object.prototype.toString()来检测对象的类型

    1.使用toString()方法来检测对象类型 可以通过toString() 来获取每个对象的类型.为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Fun ...

  4. JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()

    1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...

  5. js中[object Object]与object.prototype.toString.call()

    最近在用node读取文件中的json数据后,用JSON.parse()转成了json,然后响应数据传给前端,发现输出值object对象时显示[object object],在这里我们来看一下他的具体意 ...

  6. js中通过Object.prototype.toString方法----精确判断对象的类型

    判断是否为函数 function isFunction(it) {        return Object.prototype.toString.call(it) === '[object Func ...

  7. js Object.prototype.toString.call()

    Object.prototype.toString.call(obj)使用方法以及原理   这几天看vue-router的源码 发现了Object.prototype.toString.call()这 ...

  8. js精确判断数据类型为何用Object.prototype.toString.call()而不是Object.prototype.toString()

    有何区别,为何一定要通过call. 我们知道call是用来改变函数作用域的,Object.prototype.toString.call在这儿也是用来改变作用域的. Object.prototype. ...

  9. JS基础-数据类型判断typeof、instanceof、Object.prototype.toString

    typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...

随机推荐

  1. android开发学习---linux下开发环境的搭建&& android基础知识介绍

    一.配置所需开发环境 1.基本环境配置 JDK 5或以上版本(仅有JRE不够) (http://www.oracle.com/technetwork/java/javase/downloads/ind ...

  2. Bean的加载过程

    参考地址: http://blog.csdn.net/jy0902/article/details/50519115 http://blog.csdn.net/architect0719/articl ...

  3. kubelet源码分析(version: git tag 1.7.6)

    一.概述 kubelet源码入口:cmd/kubelet/kubelet.go main() cmd/kubelet/app 包中的Run函数: 查看先参数,kubelet.KubeletDeps t ...

  4. jQuery $.ajax 参数说明及调用注意事项

    $.ajax参数设置 备注:data.contentType.dataType属性值很重要!!! async 类型:Boolean 默认值: true.默认设置下,所有请求均为异步请求.如果需要发送同 ...

  5. Cross compiling coreutils and generate the manpages

    When we cross compiling coreutils, there is an problem of generating man pages, because the source s ...

  6. alibaba fastjson TypeReference 通过字符串反射返回对象

    TypeReferenceEditNew Page温绍 edited this page Nov 3, 2017 · 8 revisions1. 基础使用在fastjson中提供了一个用于处理泛型反序 ...

  7. 如何上传代码到github?

    如何上传代码到github? 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安 ...

  8. MySQL 两个死锁样例

    [引子] 从事MySQL-DBA这一行也有些年头了,想对新人说,在分析死锁问题时应该还要考虑到有一个叫请求队列的“概念”.之所以 在这里提这个不是因为新手不知道,而是有时候会自然而然的想不到. 不信的 ...

  9. 更新django到2.x

    django-2.x 已经发布了.想体验一下它的新功能.于是把之前电脑上的django-1.11.7uninstall 掉 一.卸载django-1.11.7: pip3 uninstall djan ...

  10. Processing支持中文显示

    Processing 默认不支持中文,中文显示成框框,我使用的版本是:2.2.1,进行如下设置,并且重启processing就可以支持中文了: 可以看到中文了: