1.Object.prototype.toString用来判断类型再合适不过,借用它几乎可以判断所有类型的数据。
console.log(Object.prototype.toString.call({}))//[object Object]
console.log(Object.prototype.toString.call([]))//[object Array]
console.log(Object.prototype.toString.call(function(){}))//[object Function]
console.log(Object.prototype.toString.call(new Set()))//[object Set]
console.log(Object.prototype.toString.call(new Map()))//[object Map]
console.log(Object.prototype.toString.call(null))//[object Null]
console.log(Object.prototype.toString.call())//[object Undefined]
一个类型判断函数:
function isType(data, type) {
const typeObj = {
'[object String]': 'string',
'[object Number]': 'number',
'[object Boolean]': 'boolean',
'[object Null]': 'null',
'[object Undefined]': 'und