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.
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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 為了保證可讀性,本文采...
閱讀 3035·2023-04-25 20:09
閱讀 3318·2021-11-23 09:51
閱讀 1971·2021-11-22 15:25
閱讀 3348·2021-11-18 10:02
閱讀 2747·2021-09-27 13:56
閱讀 1304·2019-08-30 15:44
閱讀 1149·2019-08-30 13:21
閱讀 3322·2019-08-30 11:05