资讯专栏INFORMATION COLUMN

The Abstract Equality Comparison Algorithm

Karuru / 2521人阅读

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.

NOTE 1
Given the above definition of equality:

String comparison can be forced by: "" + a == "" + b.

Numeric comparison can be forced by: +a == +b.

Boolean comparison can be forced by: !a == !b.

NOTE 2
The equality operators maintain the following invariants:

A != B is equivalent to !(A == B).

A == B is equivalent to B == A, except in the order of evaluation of A and B.

NOTE 3
The equality operator is not always transitive. For example, there might be two distinct String objects, each representing the same String value; each String object would be considered equal to the String value by the == operator, but the two String objects would not be equal to each other. For Example:

new String("a") == "a" and "a" == new String("a") are both true.

new String("a") == new String("a") is false.

NOTE 4
Comparison of Strings uses a simple equality test on sequences of code unit values. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode specification. Therefore Strings values that are canonically equal according to the Unicode standard could test as unequal. In effect this algorithm assumes that both Strings are already in normalised form.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85436.html

相关文章

  • 你真的理解==和===的区别吗?

    摘要:上面的理解是错的,和返回就可以推翻。解释不清楚和是相等的。和的规则类似,唯一少了转换的一步。三高级阶段参考规范真正理解真的如高设所说的那样吗其实不然。来分析一个经典的例子,看完彻底理解的强制转换。 showImg(https://segmentfault.com/img/remote/1460000011658462?w=512&h=321); 用中文怎么叫合适?相等?全等?其实并不合...

    TwIStOy 评论0 收藏0
  • Advanced JS Notebook

    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()=>{ ...

    jimhs 评论0 收藏0
  • 前端面试中遇到 [] == ![] ? 刨祖坟式博客解析,从 ECMAScript 规范说起,比脱下

    摘要:这种情况,它们返回一个布尔型值。语法描述逻辑非如果能转换为,返回如果能转换为,则返回。转中能够转换为的字面量是可枚举的,包含空字符串。 博客 github 地址: https://github.com/HCThink/h-blog/blob/master/interesting/in5.md github 首页(star+watch,一手动态直达): https://github....

    codeGoogle 评论0 收藏0
  • 重读Javascript之Object

    摘要:对象是中最常的内置对象之一。为了节省内存,使用一个共享的构造器使用更安全的引用如果不是或,抛出一个异常。使创建的一个新的对象为,就和通过表达式创建一个新对象一样,是标准内置的构造器名设置的内部属性为。方法返回一个该对象的字符串表示。 Object 对象是Javascript中最常的内置对象之一。除了null 和 undefined,其他的所有的都可以转换为对象。可以把对象看成含有键值一...

    Alex 评论0 收藏0
  • 怪异的JavaScript系列(一)

    摘要:是一门伟大的语言,它拥有非常简洁的语法,庞大的生态系统,以及最重要的有一个伟大的社区支撑着。可是因为它已经被广泛使用,所以委员会觉得保留它,但是违背了的规范。其实最小的数是,尽管它不是一个实际存在的数。 译者按: JavaScript有很多坑,经常一不小心就要写bug。 原文: What the f*ck JavaScript? 译者: Fundebug 为了保证可读性,本文采...

    darry 评论0 收藏0

发表评论

0条评论

Karuru

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<