先来看这个例子,请问下面表达式的值是多少。
0 == null
如果你不确定答案,或者想知道语言内部怎么处理,就可以去查看规格,7.2.12小节是对相等运算符(==
)的描述。
规格对每一种语法行为的描述,都分成两部分:先是总体的行为描述,然后是实现的算法细节。相等运算符的总体描述,只有一句话。
"The comparison
x == y
, wherex
andy
are values, producestrue
orfalse
."
上面这句话的意思是,相等运算符用于比较两个值,返回true
或false
。
下面是算法细节。
- ReturnIfAbrupt(x).
- ReturnIfAbrupt(y).
- If
Type(x)
is the same asType(y)
, then
Return the result of performing Strict Equality Comparisonx === y
.- If
x
isnull
andy
isundefined
, returntrue
.- If
x
isundefined
andy
isnull
, returntrue
.- If
Type(x)
is Number andType(y)
is String,
return the result of the comparisonx == ToNumber(y)
.- If
Type(x)
is String andType(y)
is Number,
return the result of the comparisonToNumber(x) == y
.- If
Type(x)
is Boolean, return the result of the comparisonToNumber(x) == y
.- If
Type(y)
is Boolean, return the result of the comparisonx == ToNumber(y)
.- If
Type(x)
is either String, Number, or Symbol andType(y)
is Object, then
return the result of the comparisonx == ToPrimitive(y)
.- If
Type(x)
is Object andType(y)
is either String, Number, or Symbol, then
return the result of the comparisonToPrimitive(x) == y
.- Return
false
.
上面这段算法,一共有12步,翻译如下。
- 如果
x
不是正常值(比如抛出一个错误),中断执行。- 如果
y
不是正常值,中断执行。- 如果
Type(x)
与Type(y)
相同,执行严格相等运算x === y
。- 如果
x
是null
,y
是undefined
,返回true
。- 如果
x
是undefined
,y
是null
,返回true
。- 如果
Type(x)
是数值,Type(y)
是字符串,返回x == ToNumber(y)
的结果。- 如果
Type(x)
是字符串,Type(y)
是数值,返回ToNumber(x) == y
的结果。- 如果
Type(x)
是布尔值,返回ToNumber(x) == y
的结果。- 如果
Type(y)
是布尔值,返回x == ToNumber(y)
的结果。- 如果
Type(x)
是字符串或数值或Symbol
值,Type(y)
是对象,返回x == ToPrimitive(y)
的结果。- 如果
Type(x)
是对象,Type(y)
是字符串或数值或Symbol
值,返回ToPrimitive(x) == y
的结果。- 返回
false
。
由于0
的类型是数值,null
的类型是Null(这是规格4.3.13小节的规定,是内部Type运算的结果,跟typeof
运算符无关)。因此上面的前11步都得不到结果,要到第12步才能得到false
。
0 == null // false