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.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/85436.html
摘要:上面的理解是錯(cuò)的,和返回就可以推翻。解釋不清楚和是相等的。和的規(guī)則類(lèi)似,唯一少了轉(zhuǎn)換的一步。三高級(jí)階段參考規(guī)范真正理解真的如高設(shè)所說(shuō)的那樣嗎其實(shí)不然。來(lái)分析一個(gè)經(jīng)典的例子,看完徹底理解的強(qiáng)制轉(zhuǎn)換。 showImg(https://segmentfault.com/img/remote/1460000011658462?w=512&h=321); 用中文怎么叫合適?相等?全等?其實(shí)并不合...
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()=>{ ...
摘要:這種情況,它們返回一個(gè)布爾型值。語(yǔ)法描述邏輯非如果能轉(zhuǎn)換為,返回如果能轉(zhuǎn)換為,則返回。轉(zhuǎn)中能夠轉(zhuǎn)換為的字面量是可枚舉的,包含空字符串。 博客 github 地址: https://github.com/HCThink/h-blog/blob/master/interesting/in5.md github 首頁(yè)(star+watch,一手動(dòng)態(tài)直達(dá)): https://github....
摘要:對(duì)象是中最常的內(nèi)置對(duì)象之一。為了節(jié)省內(nèi)存,使用一個(gè)共享的構(gòu)造器使用更安全的引用如果不是或,拋出一個(gè)異常。使創(chuàng)建的一個(gè)新的對(duì)象為,就和通過(guò)表達(dá)式創(chuàng)建一個(gè)新對(duì)象一樣,是標(biāo)準(zhǔn)內(nèi)置的構(gòu)造器名設(shè)置的內(nèi)部屬性為。方法返回一個(gè)該對(duì)象的字符串表示。 Object 對(duì)象是Javascript中最常的內(nèi)置對(duì)象之一。除了null 和 undefined,其他的所有的都可以轉(zhuǎn)換為對(duì)象??梢园褜?duì)象看成含有鍵值一...
摘要:是一門(mén)偉大的語(yǔ)言,它擁有非常簡(jiǎn)潔的語(yǔ)法,龐大的生態(tài)系統(tǒng),以及最重要的有一個(gè)偉大的社區(qū)支撐著??墒且?yàn)樗呀?jīng)被廣泛使用,所以委員會(huì)覺(jué)得保留它,但是違背了的規(guī)范。其實(shí)最小的數(shù)是,盡管它不是一個(gè)實(shí)際存在的數(shù)。 譯者按: JavaScript有很多坑,經(jīng)常一不小心就要寫(xiě)bug。 原文: What the f*ck JavaScript? 譯者: Fundebug 為了保證可讀性,本文采...
閱讀 3244·2023-04-25 20:09
閱讀 3372·2021-11-23 09:51
閱讀 2020·2021-11-22 15:25
閱讀 3459·2021-11-18 10:02
閱讀 2835·2021-09-27 13:56
閱讀 1370·2019-08-30 15:44
閱讀 1208·2019-08-30 13:21
閱讀 3382·2019-08-30 11:05