ES6 规格之相等运算符
相等运算符
相等运算符(==
)是一个很让人头痛的运算符,它的语法行为多变,不符合直觉。这个小节就看看规格怎么规定它的行为。
请看下面这个表达式,请问它的值是多少。
0 == null
如果你不确定答案,或者想知道语言内部怎么处理,就可以去查看规格,7.2.12 小节是对相等运算符(==
)的描述。
ES6 规格对每一种语法行为的描述,都分成两部分:先是总体的行为描述,然后是实现的算法细节。相等运算符的总体描述,只有一句话。
“The comparisonx == 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
ES6 规格之相等运算符相关推荐
- (...)ES6三点扩展运算符
来源:https://www.cnblogs.com/rlann/p/7222150.html (...)ES6三点扩展运算符 扩展运算符将一个数组转为用逗号分隔的参数序列 console.log(. ...
- ES6规格之数组的空位
数组的空位 下面再看另一个例子. const a1 = [undefined, undefined, undefined]; const a2 = [, , ,]; a1.length // 3 a2 ...
- Vue的三个点es6知识,扩展运算符表达含义
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...
- ES6高级:扩展运算符,箭头函数,class类,iterator迭代器
2. 扩展运算符 ... 2.1 reset参数 reset参数==必须放在最后==,为了替代arguments,在函数形参中使用,==接受剩余参数==,以==数组==的形式去接受 //1. rese ...
- ES6增加了扩展运算符: ... 三个点是ES几的
ES6 的扩展运算符( spread )是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. 1.将一个数组转为用逗号分隔的参数序列(把数组中元素展开). 如下: ...
- es6语法-对象拓展运算符
对象拓展运算符 创建 对象拓展运算符.html 拓展运算符(...)用于取出参数对象所有可遍历属性然后拷贝到当前对象. // 1.拷贝对象 let person1 = {name: "Amy ...
- es6 对象的扩展运算符
对象的扩展运算符 <数组的扩展>一章中,已经介绍过扩展运算符(...). const [a, ...b] = [1, 2, 3]; a // 1 b // [2, 3] ES2017 将这 ...
- ES6中的扩展运算符
扩展运算符(...)将一个数组转化为参数序列,通常与函数一起使用,show(...['judy','girl']). 数组合并:[...arr1,...arr2,...arr3] 字符串转字符数组:[ ...
- ES6对象的扩展运算符
1.对象的扩展运算符(-)用于取出参数对象的所有可遍历属性,拷贝到当前对象之中 let z = {a:3,b:4}; let n = {...z}; console.log(n);//{a:3,b:4 ...
最新文章
- webstrom常用键
- set OpenCV_ROOT
- JPA_could not extract ResultSet问题解决
- 细腻剖析2010年3月四级网络工程师试卷解题思路
- 都说开完电动汽车,就不想再开回燃油车了,是这样吗?
- 了解 Adobe Scout 收集和使用的数据
- HTML样式- CSS
- Opencv的KeyPoint和DMatch数据结构
- 系统运行后修改linux系统时区
- 线性代数:切线空间和法向量变换
- 2022-06-08 iPhone快捷指令自动切换壁纸
- 报错:Apache提示 the requested operation has failed无法启动问题(最全!)
- 2021年第十二届蓝桥杯大赛软件赛决赛C/C++大学A组 个人部分题解
- 画一个带统计检验的PCoA分析结果
- Kubernetes生产实践系列之二十二:Service Mesh之在Kubernetes部署Linkerd2进行service mesh
- 蓝屏解决办法,及介绍。(装逼或学习)
- 《JavaEE开发技术》课程考试试题(A卷)
- NLD4J - Arbiter
- ShareSDK Android SDK API
- 网页版终端webssh2配置