typeof作用
返回一个字符串,表示未经计算的操作数的类型
typeof能够分辨哪几种类型
number,boolean,string,undefined,symbol,bigint,object,function
typeof是否可以看做识别基本类型。
基本类型:number,boolean,string,undefined,null,symbol,bigint
- typeof可以识别object,object不是基本类型
- typeof可以识别function,function不是基本类型
- typeof把null识别成了object
- typeof识别包装类型为object
typeof的特别之处
它可以识别未定义的元素不会报错,可以把包装类辨识为对象。
typeof缺点
把包装类辨识为对象,无法辨识基本类型null,无法辨识引用类型Array,Date,RegExp
使用typeof常见错误
- typeof null === ‘object’
- typeof [1,2] === ‘object’
- typeof new Boolean(1) === ‘object’
- typeof Boolean(1) === ‘boolean’
- typeof Boolean === ‘function’
- typeof new String(’23’) === ‘object’
- typeof Date() === ‘string’
- typeof class C {} === ‘function’
- typeof /a/ === ‘object’
- typeof Infinity === ‘number’
- typeof new Function === ‘function’