数据类型判断函数

function queryType(val) {
 let result = Object.prototype.toString.call(val)
 result = /[A-Z]{1}[w]+/.exec(result)[0].toLowerCase()
 return result
}

demo:

type(0) // 数字
type('') // 字符串
type(new Date()) // 日期
type(Symbol()) // Symbol
type(function() {}) // 函数
type({}) // 对象
type([]) // 数组
type(null) // null
type(undefined) // unddefined
type(false) // 布尔
type(new Set()) // set
type(new Map()) // map
type(new Promise(resolve => {})) // promise
type(/[s]+/g) // 正则

打印结果:

number
string
date
symbol
function
object
array
null
undefined
boolean
set
map
promise
regexp
原文地址:https://www.cnblogs.com/liea/p/13129964.html