function typeOf(obj) {
let toString = Object.prototype.toString.call(obj).slice(8, -1)
let type = toString.charAt(0).toLowerCase() + toString.slice(1) || ''
return type
}
console.log(typeOf(1)) // number
console.log(typeOf('1')) // string
console.log(typeOf(true)) // boolean
console.log(typeOf(null)) // null
console.log(typeOf(undefined)) // undefined
console.log(typeOf(Symbol(1))) // symbol
console.log(typeOf({})) // object
console.log(typeOf([])) // array
console.log(typeOf(function(){})) // function
console.log(typeOf(new Date())) // date
console.log(typeOf(/at/)) // regExp
封装一个方法:通过对象的原型方法判断数据类型
于 2023-07-24 18:00:07 首次发布