JS判断类型的方式
- 
1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11typeof '' // 'string' 
 typeof {} // 'object'
 typeof null // 'object'
 typeof [] // 'object'
 typeof 5 // 'number'
 typeof NaN // 'number'
 typeof (() => {}) // 'function'
 typeof true // 'boolean'
 typeof undefined // 'undefined'
 typeof Symbol() // 'symbol'
 typeof 42n // 'bigint'
Symbol,可以用作对象中的键。BigInt,一种方法来表示大于 253 - 1 的整数。
在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。由于
null代表的是空指针(大多数平台下值为 0x00),因此,null 的类型标签是 0,typeof null也因此返回"object"。
new 操作符: 除 Function 外的所有构造函数的类型都是 ‘object’
| 1 | var str = new String('String'); | 
加入了块级作用域的 let 和 const 之后,在其被声明之前对块中的
let和const变量使用typeof会抛出一个 ReferenceError。
| 1 | typeof newLetVariable; // ReferenceError | 
instanceof运算符用来检测constructor.prototype是否存在于参数object的原型链上。
| 1 | var a = {} | 
| 1 | ({}).constructor === Object // true | 
- Object.prototype.toString
| 1 | Object.prototype.toString.call(null) // "[object Null]" | 
JavaScript 标准文档中定义: [[Class]] 的值只可能是下面字符串中的一个: Arguments, Array, Boolean, Date, Error, Function, JSON, Math, Number, Object, RegExp, String.
为什么是Object.prototype.toString?而不是obj.toString?
因为toString可能被改写。
- 其他方法
| 1 | Array.isArray([]) // true | 


