11.9.3 The Abstract Equality Comparison Algorithm
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is −0, return true.
If x is −0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false.
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 or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
Return false.
String comparison can be forced by: "" + a == "" + b.
Numeric comparison can be forced by: +a == +b.
Boolean comparison can be forced by: !a == !b.
A != B is equivalent to !(A == B).
A == B is equivalent to B == A, except in the order of evaluation of A and B.
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/85436.html
摘要:上面的理解是错的,和返回就可以推翻。解释不清楚和是相等的。和的规则类似,唯一少了转换的一步。三高级阶段参考规范真正理解真的如高设所说的那样吗其实不然。来分析一个经典的例子,看完彻底理解的强制转换。 showImg(https://segmentfault.com/img/remote/1460000011658462?w=512&h=321); 用中文怎么叫合适?相等?全等?其实并不合...
How JavaScript works? JavaScript is a single-threaded language that can be non-blocking. showImg(https://segmentfault.com/img/bVbiqTf?w=1678&h=852); JavaScript Engine For the code below: const f()=>{ ...
摘要:这种情况,它们返回一个布尔型值。语法描述逻辑非如果能转换为,返回如果能转换为,则返回。转中能够转换为的字面量是可枚举的,包含空字符串。 博客 github 地址: https://github.com/HCThink/h-blog/blob/master/interesting/in5.md github 首页(star+watch,一手动态直达): https://github....
摘要:对象是中最常的内置对象之一。为了节省内存,使用一个共享的构造器使用更安全的引用如果不是或,抛出一个异常。使创建的一个新的对象为,就和通过表达式创建一个新对象一样,是标准内置的构造器名设置的内部属性为。方法返回一个该对象的字符串表示。 Object 对象是Javascript中最常的内置对象之一。除了null 和 undefined,其他的所有的都可以转换为对象。可以把对象看成含有键值一...
摘要:是一门伟大的语言,它拥有非常简洁的语法,庞大的生态系统,以及最重要的有一个伟大的社区支撑着。可是因为它已经被广泛使用,所以委员会觉得保留它,但是违背了的规范。其实最小的数是,尽管它不是一个实际存在的数。 译者按: JavaScript有很多坑,经常一不小心就要写bug。 原文: What the f*ck JavaScript? 译者: Fundebug 为了保证可读性,本文采...
阅读 2997·2023-04-25 20:09
阅读 3302·2021-11-23 09:51
阅读 1957·2021-11-22 15:25
阅读 3330·2021-11-18 10:02
阅读 2733·2021-09-27 13:56
阅读 1289·2019-08-30 15:44
阅读 1137·2019-08-30 13:21
阅读 3294·2019-08-30 11:05