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';
    }
}

最新文章

  1. JVM内存模型、指令重排、内存屏障概念解析
  2. Spike Notes on Lock based Concurrency Concepts
  3. ORACLE常见数据类型详解
  4. 学Python后到底能干什么?
  5. bll编译错误
  6. android手机连接PC无法正常安装驱动
  7. windows通过thrift访问hdfs
  8. TIOBE 2015年5月编程语言排行榜 Visual Studio系列在上升
  9. 基于asp.net的ajax分页
  10. CentOS 6.4利用xampp安装bugfree3
  11. ruby eclipse调试
  12. 深度学习Matlab DeepLearningToolBox 工具包最常见错误解决办法\
  13. 典型c库函数的实现
  14. 关于JS函数的bind
  15. C++套接字类CxUdpSocket的设计
  16. Prism for WPF再探(基于Prism事件的模块间通信)
  17. 一、OpenStack环境准备及共享组件安装
  18. toastr操作完成提示框
  19. 机器学习笔记(三)Logistic回归模型
  20. jQuery on() 方法 为选定已存在元素和未来元素绑定标准事件和自定义事件

热门文章

  1. 转:一篇很全面的freemarker教程
  2. 老男孩linux实战培训初级班第二次课前考试题
  3. MySQL安装Altas
  4. 如何从40亿整数中找到不存在的一个 webservice Asp.Net Core 轻松学-10分钟使用EFCore连接MSSQL数据库 WPF实战案例-打印 RabbitMQ与.net core(五) topic类型 与 headers类型 的Exchange
  5. Android水波纹特效的简单实现
  6. iphone app的非appstore发布方法及其免越狱安装方法
  7. Internet上的WWW服务与HTTP协议(非常非常不错的文档,推荐订阅)
  8. 【转载并整理】ORACLE锁机制
  9. [转] JDBC中的Statement和PreparedStatement的区别
  10. sql综合查询with row_number() over 递归和开窗查询