用Object.prototype.toString做数据类型判断

这几天在看js文档,有个数据类型判断的方法可以记录一下,方便以后翻阅使用,完整方案放在了文末。
数据类型 【用Object.prototype.toString做数据类型判断】js中有6中数据类型,es6新增了一种。
原始数据类型:Number、String、Boolean、null、undefined;
引用数据类型:Object;
es6新增类型:Symbol。
Object里又可以细分function、object、array、regexp、date等。
用toString()判断数据类型 Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型:

var obj = {}; obj.toString() // "[object Object]"

上面代码调用空对象的toString方法,结果返回一个字符串object Object,其中第二个Object表示该值的构造函数。这是一个十分有用的判断数据类型的方法。
由于实例对象可能会自定义toString方法,覆盖掉Object.prototype.toString方法,所以为了得到类型字符串,最好直接使用Object.prototype.toString方法。通过函数的call方法,可以在任意值上调用这个方法,帮助我们判断这个值的类型。
Object.prototype.toString.call(value)

上面代码表示对value这个值调用Object.prototype.toString方法。
不同数据类型的Object.prototype.toString方法返回值如下。
数值:返回[object Number]。
字符串:返回[object String]。
布尔值:返回[object Boolean]。
undefined:返回[object Undefined]。
null:返回[object Null]。
symbol:返回[object Symbol]。
数组:返回[object Array]。
函数:返回[object Function]。
Date 对象:返回[object Date]。
RegExp 对象:返回[object RegExp]。
arguments 对象:返回[object Arguments]。
Error 对象:返回[object Error]。
Math 对象:返回[object Math]。
其他对象:返回[object Object]。
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(Symbol()) // "[object Symbol]" Object.prototype.toString.call([]) // "[object Array]" Object.prototype.toString.call(function(){}) // "[object Function]" Object.prototype.toString.call(new Date()) // "[object Date]" Object.prototype.toString.call(/abc/) // "[object RegExp]" (function(){return (Object.prototype.toString.call(arguments))})() // "[object Arguments]" Object.prototype.toString.call(new Error) // "[object Error]" Object.prototype.toString.call(Math) // "[object Math]" Object.prototype.toString.call({}) // "[object Object]"

利用这个特性,可以写出一个比typeof运算符更准确的类型判断函数。
const type = function (o){ const 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"

完整方法
const type = (function() { const obj = { type_arr: [ "Number", "String", "Boolean", "Undefined", "Null", "Symbol", "Array", "Function", "Date", "RegExp", "Arguments", "Error", "Math", "Object", ], getType: function(o) { const s = Object.prototype.toString.call(o); return s.match(/\[object (.*?)\]/)[1].toLowerCase(); } }; obj.type_arr.forEach(t => { obj['is' + t] = function (o) { return obj.getType(o) === t.toLowerCase(); } }); return obj; })(); type.getType(123); // number type.isObject({}); // true type.isNumber(123); // true type.isRegExp(/abc/); // true

参考:
Object 对象

    推荐阅读