首页 > 其他 > 详细

数据类型判断函数

时间:2020-06-15 13:56:59      阅读:41      评论:0      收藏:0      [点我收藏+]
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!