数据类型检测四种方法-typeof
2020-12-04 本文已影响0人
撑船的摆渡人
ECMAScript-262
12.5.6.1 Runtime Semantics: Evaluation
UnaryExpression : typeof UnaryExpression
Let val be the result of evaluating UnaryExpression.
If Type(val) is Reference, then
If IsUnresolvableReference(val) is true, return “undefined”.
Let val be GetValue(val).
ReturnIfAbrupt(val).
Return a String according to Table 35.
一元表达式: typeof 一元表达式
- 令val为一元表达式评估的结果。
- 如果类型(val)是引用,则
a. 如果IsUnresolvableReference (val)为true,则返回"undefined"。- 令val为GetValue (val)。
- ReturnIfAbrupt (val)。
- 根据表35返回一个字符串。
![](https://img.haomeiwen.com/i13991253/79db6f1cf49d94a3.png)
NOTE
Implementations are discouraged from defining new typeof result values for non-standard exotic objects. If possible "object"should be used for such objects.
不鼓励实现typeof为非标准奇异对象定义新的结果值。如果可能,"object"应将此类对象用于该对象。
ECMAScript 提供的内置类型在计算机底层都是按照二进制数据存储的
以对应的数字开始代表不同的类型
1: 数字 010: 浮点数
100: 字符串
110: 布尔
-2^30: undefined
000000: null
000: 对象
typeof null ===> 'object'
因为以三个零开始的都是对象,所以null也是对象,这个属于设计上的缺陷
typeof null ===> 'object'
typeof 实现call的对象 [函数、箭头函数、生成器函数、构造函数] ===> 'function'
typeof 剩下未实现call 的对象 ===> 'object'
typeof typeof [] ===> 'string' // 因为typeof 返回的都是string类型,前面不管还有几个typeof 肯定都是string