1. 基本数据类型和引用数据类型
| 类型区分 | 基本数据类型 | 引用(复杂)数据类型 |
|---|---|---|
| String | ||
| Boolean | ||
| Number | ||
| undefined | ||
| NULL | ||
| Symbol | ||
| BigInt | ||
| Function | ||
| Array | ||
| 正则表达式RegExp | ||
| Date | ||
| Object | ||
| 区别 | 数据存放在「栈」中 | 「地址」存放在「栈」中 「数据」存放在「堆」中 |
2. 判断数据类型
| typeof | instanceof | constructor | Object.prototype.toString | |
|---|---|---|---|---|
| typeof '5' = string | ‘5’.proto.constructor == String // true | Object.prototype.toString.call(‘5’)===“[object, String]” | ||
| typeof true = boolean | true.proto.constructor == Boolean //T | Object.prototype.toString.call(true) === “[object, Boolean ]” | ||
| typeof 5 = number | 5.proto.constructor == Number //T | Object.prototype.toString.call(5)=== “[object, Number ]” | ||
| typeof undefined= undefined | - | Object.prototype.toString.call(undefined) ===“[object, undefined]” | ||
| typeof null = object | - | Object.prototype.toString.call(null) === “[object, Null]” | ||
| typeof Symbol() = symbol | Symbol().proto.constructor == Symbol //T | Object.prototype.toString.call(Symbol()) === “[object Symbol]” | ||
| typeof 1n = bigint | 1n.proto.constructor == BigInt //T | Object.prototype.toString.call(1n) === “[object BigInt]” | ||
| typeof fn = function | fn.proto.constructor == Function //T | Object.prototype.toString.call(fn) === “[object Function]” | ||
| typeof [] = object | [] instanceof Array //T [] instanceof Object //T | [].proto.constructor == Array //T <[].proto.constructor == Object // F | Object.prototype.toString.call([])=== “[object Array]” | |
| typeof /s/ = object | /s/ instanceof RegExp //T /s/ instanceof Object //T | /s/.proto.constructor == RegExp //T /s/.proto.constructor == Object //F | Object.prototype.toString.call(/s/)=== “[object RegExp ]” | |
| typeof {} = object | {} instanceof Object //T | {}.proto.constructor == Object //T | Object.prototype.toString.call({})=== “[object Object ]” | |
| var x = new Date() typeof x = object | x instanceof Date //T x instanceof Object //T | x.proto.constructor == Date //T x.proto.constructor == Object //F | Object.prototype.toString.call(x)=== “[object Date]” | |
| 返回「类型」 | 返回「布尔值」 | 返回「布尔值」 | 返回对象的类型字符串 | |
| 变量的原型链上是否有构造函数的prototype属性 | 每个实例对象可通过constructor访问构造函数 |
Ps: 判断数组: Array.isArray( [] ) 、****
Object.getPrototypeOf(arr) === Array.prototype