Standard ECMA-262 6th Edition 對寬鬆相等(==)的定義以下:html
Abstract Equality Comparison
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
git
- 若x與y的類型相同, 則返回x===y的結果
- 若x=null,y=undefined,則return true。
- 若x=undefined,y=null,則return true。
- 若x的類型爲Number,y的類型爲String,則return x == ToNumber(y)的執行結果。
- 若x的類型爲String,y的類型爲Number,則return ToNumber(x) == y的執行結果。
- 若x的類型爲Boolean, 則return ToNumber(x) == y的執行結果。
- 若y的類型爲Boolean, 則return x == ToNumber(y)的執行結果。
- 若x的類型爲String, Number或者Symboly,y的類型爲 Object, 則 return x == ToPrimitive(y)的執行結果。
- 若x的類型爲Object,y的類型爲String, Number, 或者Symbol, 則 return ToPrimitive(x) == y的執行結果。
- 若以上都不知足則return false
參數類型 | 結果 |
---|---|
Undefined | NaN |
Null | 0 |
Bollean | true => 1 fale => 0 |
Symbol | Throw a TypeError exception |
String | 若是字符串能被解析爲StringNumericLiteral(定義以下),則將字符串轉換爲相應的數字,不然ToNumber的結果爲NaN |
Object | 1.先執行ToPrimitive操做,將object轉換爲基本類型 2.執行ToNumber操做 |
StringNumericLiteral :::
StrWhiteSpace
StrWhiteSpaceopt StrNumericLiteral StrWhiteSpaceopt
StrWhiteSpace :::
StrWhiteSpaceChar StrWhiteSpaceopt
StrWhiteSpaceChar :::
WhiteSpace
LineTerminator
StrNumericLiteral ::::
StrDecimalLiteral
BinaryIntegerLiteral
OctalIntegerLiteral
HexIntegerLiteral
StrDecimalLiteral :::
StrUnsignedDecimalLiteral
+StrUnsignedDecimalLiteral
-StrUnsignedDecimalLiteral
StrUnsignedDecimalLiteral :::
Infinity
DecimalDigits . DecimalDigitsopt ExponentPartopt
. DecimalDigits ExponentPartopt DecimalDigits ExponentPartopt
DecimalDigits :::
DecimalDigit
DecimalDigits DecimalDigit
DecimalDigit ::: one of
0 1 2 3 4 5 6 7 8 9
ExponentPart :::
ExponentIndicator SignedInteger
ExponentIndicator ::: one of
e E
SignedInteger :::
DecimalDigits
+DecimalDigits
-DecimalDigits
數組
ToPrimitive根據PreferredType將input轉換爲primitive基本類型,在ES6中用戶可自定義toPrimitive方法。app
不一樣類型對象的valueOf()方法的返回值函數
對象 | 返回值 |
---|---|
Array | 返回數組對象自己。 |
Boolean | 布爾值。 |
Date | 存儲的時間是從 1970 年 1 月 1 日午夜開始計的毫秒數 UTC。 |
Function | 函數自己。 |
Number | 數字值。 |
Object | 對象自己。這是默認狀況。 |
String | 字符串值。 |
Math 和 Error 對象沒有 valueOf 方法。 |
參數類型 | 結果 |
---|---|
Undefined | 'undefined' |
Null | 'null' |
Bollean | true =>'true' fale =>'fale' |
Symbol | Throw a TypeError exception |
Number | NaN => 'NaN' +0 or -0 => '0' +∞ => Infinity |
Object | 1.先執行ToPrimitive操做,將object轉換爲基本類型 2.執行ToString操做 |
參數類型 | 結果 |
---|---|
Undefined | false |
Null | false |
String | 空字符串 =》false,其餘爲true |
Symbol | true |
Number | +0, −0, or NaN => false,其餘爲true |
Object | true |