ES6结合正则判断js数据类型
2021-02-16 10:20
标签:date cal name regex es6 数据类型 cti 方式 判断 ES5:(有重复问题) ES6: (修补了问题) 最后ES6结合正则的方式,使用最方便: 注意首字母是大写 . ES6结合正则判断js数据类型 标签:date cal name regex es6 数据类型 cti 方式 判断 原文地址:https://www.cnblogs.com/xiangsj/p/12971623.htmltypeof 1 ---> ‘number‘
typeof ‘hello‘ ---> ‘string‘
typeof alert ---> ‘function‘
typeof [1,2,3] ---> ‘object‘
typeof {a:1,b:2} ---> ‘object‘
typeof null ---> ‘object‘
typeof NaN ---> ‘number‘
typeof undefined ---> ‘undefined‘
typeof Symbol() ---> ‘symbol‘
console.log(Object.prototype.toString.call("hello"));//[object String]
console.log(Object.prototype.toString.call(123));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function foo(){}
console.log(Object.prototype.toString.call(new foo()));//[object Object]
const isType = type =>(/^\[object\s(.*)\]$/.exec(Object.prototype.toString.call(type)))[1];
isType({}) // ‘Object‘
isType([]) // ‘Array‘