标签:date cal name regex es6 数据类型 cti 方式 判断
ES5:(有重复问题)
typeof 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‘
ES6: (修补了问题)
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]
最后ES6结合正则的方式,使用最方便:
const isType = type =>(/^\[object\s(.*)\]$/.exec(Object.prototype.toString.call(type)))[1];
isType({}) // ‘Object‘
isType([]) // ‘Array‘
注意首字母是大写
.
标签:date cal name regex es6 数据类型 cti 方式 判断
原文地址:https://www.cnblogs.com/xiangsj/p/12971623.html