您的位置:首页 > 其它

ES6 规格之相等运算符

2018-02-06 22:28 113 查看


相等运算符

相等运算符(
==
)是一个很让人头痛的运算符,它的语法行为多变,不符合直觉。这个小节就看看规格怎么规定它的行为。

请看下面这个表达式,请问它的值是多少。
0 == null


如果你不确定答案,或者想知道语言内部怎么处理,就可以去查看规格,7.2.12 小节是对相等运算符(
==
)的描述。

ES6 规格对每一种语法行为的描述,都分成两部分:先是总体的行为描述,然后是实现的算法细节。相等运算符的总体描述,只有一句话。

“The comparison
x == y
, where
x
and
y
are
values, produces
true
or
false
.”

上面这句话的意思是,相等运算符用于比较两个值,返回
true
false


下面是算法细节。

ReturnIfAbrupt(x).

ReturnIfAbrupt(y).

If
Type(x)
is the same as
Type(y)
,
then\ Return the result of performing Strict Equality Comparison
x
=== y
.

If
x
is
null
and
y
is
undefined
,
return
true
.

If
x
is
undefined
and
y
is
null
,
return
true
.

If
Type(x)
is Number and
Type(y)
is
String,\ return the result of the comparison
x == ToNumber(y)
.

If
Type(x)
is String and
Type(y)
is
Number,\ return the result of the comparison
ToNumber(x) ==
y
.

If
Type(x)
is Boolean, return the result of the comparison
ToNumber(x)
== y
.

If
Type(y)
is Boolean, return the result of the comparison
x
== ToNumber(y)
.

If
Type(x)
is either String, Number, or Symbol and
Type(y)
is
Object, then\ return the result of the comparison
x == ToPrimitive(y)
.

If
Type(x)
is Object and
Type(y)
is
either String, Number, or Symbol, then\ return the result of the comparison
ToPrimitive(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: