typeof和instanceof
typeof和instanceof都可以用来将值分类,typeof主要用于原始值,instanceof主要用于对象。
typeof使用方法
1 | typeof <value> |
typeof返回描述value’数据类型’的字符串
- undefined未定义
- boolean布尔值
- string字符串
- number数组
- object对象或者null
- function函数
1 | typeof true // 'boolean' |
详情对应下表
| 操作数 | 结果 |
|---|---|
| undefined | ‘undefined’ |
| null | ‘object’ |
| Boolean value | ‘boolean’ |
| Number value | ‘number’ |
| String value | ‘string’ |
| Function | ‘function’ |
| All other value | ‘object’ |
instanceof
判断变量是否属于某个类型
1 | value instanceof <Constr> |

