javascript -- 判断是否为某个数据类型

为何不用其他方法,因为下面的写法考虑了各种兼容性。
判断是否为数组
isArray = function (source) {
    return '[object Array]' == Object.prototype.toString.call(source);
};
判断是否为日期对象
isDate = function(o) {
    // return o instanceof Date;
    return {}.toString.call(o) === "[object Date]" && o.toString() !== 'Invalid Date' && !isNaN(o);
};
判断是否为Element对象
isElement = function (source) {
    return !!(source && source.nodeName && source.nodeType == 1);
};
判断目标参数是否为function或Function实例
isFunction = function (source) {
    // chrome下,'function' == typeof /a/ 为true.
    return '[object Function]' == Object.prototype.toString.call(source);
};
判断目标参数是否number类型或Number对象
isNumber = function (source) {
    return '[object Number]' == Object.prototype.toString.call(source) && isFinite(source);
};
 判断目标参数是否为Object对象
isObject = function (source) {
    return 'function' == typeof source || !!(source && 'object' == typeof source);
};
判断目标参数是否string类型或String对象
isString = function (source) {
    return '[object String]' == Object.prototype.toString.call(source);
};
判断目标参数是否Boolean对象
isBoolean = function(o) {
    return typeof o === 'boolean';
};

原文地址:https://www.cnblogs.com/hf8051/p/4795053.html