「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战」。
数据类型分类
基本(原始)类型(primitive values)
BooleannullundefinedNumberBigIntStringSymbol
引用类型(reference value)
Object类型
数据类型判断
对如下数据进行数据类型判断
let a
const b = true
const n = 123
const str = 'abc'
const big = 2n
const bigi = BigInt(3)
const s = Symbol('s')
const foo = function () {}
const nul = null
const arr = ['a', 'b']
const obj = {x: 9}
const now = new Date()
const exp = /partten/g
1. typeof
typeof功能- 识别除
null之外的所有基本类型 - 识别函数
- 识别除
// 识别除`Null`之外的所有基本类型
console.log(`typeof a ? ${typeof a}`); // undefined
console.log(`typeof b ? ${typeof b}`); // boolean
console.log(`typeof n ? ${typeof n}`); // number
console.log(`typeof str ? ${typeof str}`); // string
console.log(`typeof big ? ${typeof big}`); // bigint
console.log(`typeof bigi ? ${typeof bigi}`); // bigint
console.log(`typeof s ? ${typeof s}`); //symbol
// 识别函数
console.log(`typeof foo ? ${typeof foo}`); // function
但是typeof无法细分null以及具体的引用类型
console.log(`typeof nul ? ${typeof nul}`); // object
console.log(`typeof arr ? ${typeof arr}`); // object
console.log(`typeof obj ? ${typeof obj}`); // object
2. instanceof
instanceof操作符可以用来确定一个对象实例的原型链上是否有原型。
按照定义,所有引用值都是Object的实例,因此通过instanceof操作符检测任何引用值和Object构造函数都会返回true。类似地,如果用instanceof检测原始值,则始终会返回false,因为原始值不是对象。
console.log(`b instanceof Boolean ? ${b instanceof Boolean}`); // false
console.log(`n instanceof Number ? ${n instanceof Number}`);// false
console.log(`str instanceof String ? ${str instanceof String}`);// false
console.log(`big instanceof BigInt ? ${big instanceof BigInt}`);// false
console.log(`s instanceof Symbol ? ${s instanceof Symbol}`);// false
console.log(`nul instanceof Object ? ${nul instanceof Object}`);// false
console.log(`foo instanceof Function ? ${foo instanceof Function}`);// true
console.log(`arr instanceof Array ? ${arr instanceof Array}`);// true
console.log(`obj instanceof Object ? ${obj instanceof Object}`);// true
此外,instanceof无法检测出null和undefined。
3. Object.prototype.toString()
可以通过 toString() 来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg。
console.log(`Object.prototype.toString.call(a) ? ${Object.prototype.toString.call(a)}`); // [object Undefined]
console.log(`Object.prototype.toString.call(b) ? ${Object.prototype.toString.call(b)}`); // [object Boolean]
console.log(`Object.prototype.toString.call(n) ? ${Object.prototype.toString.call(n)}`); // [object Number]
console.log(`Object.prototype.toString.call(str) ? ${Object.prototype.toString.call(str)}`); // [object String]
console.log(`Object.prototype.toString.call(big) ? ${Object.prototype.toString.call(big)}`); // [object BigInt]
console.log(`Object.prototype.toString.call(s) ? ${Object.prototype.toString.call(s)}`); // [object Symbol]
console.log(`Object.prototype.toString.call(nul) ? ${Object.prototype.toString.call(nul)}`); // [object Null]
console.log(`Object.prototype.toString.call(foo) ? ${Object.prototype.toString.call(foo)}`); // [object Function]
console.log(`Object.prototype.toString.call(arr) ? ${Object.prototype.toString.call(arr)}`); // [object Array]
console.log(`Object.prototype.toString.call(obj) ? ${Object.prototype.toString.call(obj)}`); // [object Object]
console.log(`Object.prototype.toString.call(now) ? ${Object.prototype.toString.call(now)}`); // [object Date]
console.log(`Object.prototype.toString.call(exp) ? ${Object.prototype.toString.call(exp)}`); // [object RegExp]
关于为什么用Object.prototype.toString()方法,推荐看一下这篇文章,受益匪浅由Object.prototype.toString.call( )引发关于toString( )方法的思考