JS隱式轉換--寬鬆相等(==)

1.隱式轉換規則

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

2.各隱式裝箱的定義

2.1 ToNumber(其餘類型轉換爲數字)

參數類型 結果
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
數組

2.2 ToPrimitive( input [, PreferredType] )

ToPrimitive根據PreferredType將input轉換爲primitive基本類型,在ES6中用戶可自定義toPrimitive方法。app

  • input(null,undefined,string,number,bool,Symbol)->直接返回input
  • input的類型爲Date類型時(若用戶沒有改寫toPrimitive)->PreferedType=string,則:
    a.調用input的toString方法,若result時primitive基本類型則返回result
    b.不然,調用input的valueOf方法,若result時primitive基本類型,則返回result
    c.不然,Throw a TypeError exception.
  • input爲非Date的引用類型時(若用戶沒有改寫toPrimitive)->PreferedType=number,則:
    a.調用input的valueOf方法,若result是primitive基本類型則返回result
    b.不然,調用input的toString方法,若result是primitive基本類型,則返回result
    c.不然,Throw a TypeError exception.

不一樣類型對象的valueOf()方法的返回值函數

對象 返回值
Array 返回數組對象自己。
Boolean 布爾值。
Date 存儲的時間是從 1970 年 1 月 1 日午夜開始計的毫秒數 UTC。
Function 函數自己。
Number 數字值。
Object 對象自己。這是默認狀況。
String 字符串值。
Math 和 Error 對象沒有 valueOf 方法。

2.3 ToString(其餘類型轉爲數字)

參數類型 結果
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操做
PS:
(1).""+value與String(value)都是運用內部函數toString將基本類型轉爲字符串的
(2).可經過 Object.prototype.toString()來檢測每一個對象的類型,但須要以Function.prototype.call()或者Function.prototype.apply()的形式來調用,如:Object.prototype.toString.call(function(){})=>"[object Function]"

2.4 ToBoolean

參數類型 結果
Undefined false
Null false
String 空字符串 =》false,其餘爲true
Symbol true
Number +0, −0, or NaN => false,其餘爲true
Object true
相關文章
相關標籤/搜索