鉴别JS数据类型的全套方法

  ECMAScript 标准定义了 7 种数据类型:Boolean、Null、Undefined、Number、String、Symbol(ES6新增)和Object,除Object以外的那6种数据类型也被称为基本数据类型,另外还有Array、Function等复杂数据类型。本文介绍一般类型判断方法,最后总给一套全面的数据类型判断方法。

一、typeof

  typeof是一个一元运算符(不是一个函数方法),可以鉴别null以外的基本数据类型以及Object和Function。它的返回值是小写的字符串:

/**** typeof ****/
typeof 37 ;  //输出 "number"
typeof "aaa" ;  //输出 "string"
typeof undefined ;  //输出 "undefined"
typeof false;  //输出 "boolean"
typeof {a:1,b:2};  //输出 "object"
typeof function(){};  //输出 "function"

//它不能鉴别null和array
typeof null;  //输出 "object"
typeof [1,2];  //输出 "object"

二、Object.prototype.toString.call()

  完美鉴别基本数据类型及Object、Function、Array、Date、Math等等类型

/**** Object.prototype.toString.call ****/
Object.prototype.toString.call("aa"); //输出 "[object String]"
Object.prototype.toString.call(123); //输出 "[object Number]"
Object.prototype.toString.call(null); //输出 "[object Null]"
Object.prototype.toString.call(undefined); //输出 "[object Undefined]"
Object.prototype.toString.call([1,2,3]); //输出 "[object Array]"
Object.prototype.toString.call(function(){}); //输出 "[object Function]"
Object.prototype.toString.call({a:1,b:2}); //输出 "[object Object]"

三、其他判断方法

  Array.isArray()可以判断一个数据是否是数组类型。

  instanceof操作符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。某些情况下也能用来检测数据类型,慎用。

/**** isArray判断数组 ****/
var arrTmp = [1,2];
Array.isArray(arrTmp);  //输出true

/**** instanceof判断类型,不准确 ****/
var numTmp1 = 123;
var numTmp2 = new Number(123);
numTmp1 instanceof Number; //输出 false
numTmp2 instanceof Number; //输出 true
arrTmp instanceof Array; //输出 true
arrTmp instanceof Object; //输出 true

四、全套判断方法

  (下面这段代码挪自Anguar源码,稍加修整)

var toString = Object.prototype.toString;

function isUndefined(value) {
    return typeof value === 'undefined';
}
function isDefined(value) {
    return typeof value !== 'undefined';
}
function isObject(value) {
    return value !== null && typeof value === 'object';
}
function isString(value) {
    return typeof value === 'string';
}
function isNumber(value) {
    return typeof value === 'number';
}
function isDate(value) {
    return toString.call(value) === '[object Date]';
}
var isArray = Array.isArray;
function isFunction(value) {
    return typeof value === 'function';
}
function isRegExp(value) {
    return toString.call(value) === '[object RegExp]';
}
function isWindow(obj) {
    return obj && obj.window === obj;
}
function isFile(obj) {
    return toString.call(obj) === '[object File]';
}
function isFormData(obj) {
    return toString.call(obj) === '[object FormData]';
}
function isBoolean(value) {
    return typeof value === 'boolean';
}
function isPromiseLike(obj) {
    return obj && isFunction(obj.then);
}
function isElement(node) {
    return !!(node && (node.nodeName || (node.prop && node.attr && node.find)));
}
function isArrayLike(obj) {
    if (obj == null || isWindow(obj)) {
        return false;
    }
    var length = "length" in Object(obj) && obj.length;
    if (obj.nodeType === 1 && length) {
        return true;
    }
    return isString(obj) || isArray(obj) || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj;
}

  

原文地址:https://www.cnblogs.com/yangshifu/p/7416214.html