2012-03-17 42 views
5

在javascript中工作時,有人可以爲我提供一個很好的參考或解釋測試相等/不等式和類型強制嗎?JavaScript中的平等

從我一直在閱讀中,我看到在使用eqeq(==)和eqeqeq(===)時有兩個思想原理,有些人覺得你不應該使用eqeq,並總是使用eqeqeq,因爲它是使用更安全。

我一直在玩弄一些基本的樣品,我無法辨別的差異,或當最好使用一個比其他:

例如:這裏是一些基本的腳本我寫出來。當我使用eqeq或eqeqeq進行測試時,我會得到相同的結果。我還沒有看到一個例子,但我會得到不同的結果(即,使用eqeq返回true,其中eqeqeq返回false)。

function write(message){ 

    document.getElementById('message').innerHTML += message +'<br/>'; 
} 


var tim = { name: "tim" }; 
var tim2 = { name: "tim" }; 
//objects are equal to themselves (== vs ==== eqeq or eqeqeq) 
write("tim eq tim: " + (tim == tim)); //returns true 

//objects are only equal to themselves regardless of containing value got that 
write("tim eq tim2: " + (tim === tim2)); //returns false 

//access the primative type to test true or false 
write("tim value eq tim2 value: " + (tim.name === tim2.name)); //returns true 
//how does this differ in efficency over the eqeq operator? is one safer to use over the other? 
//write("tim value eq tim2 value: " + (tim.name == tim2.name)); //also returns true 

//testing primatives 

write("apple eqeqeq apple: " + ("apple" === "apple")); //true 
write("apple eqeqeq apple: " + ("apple" == "apple")); //true 

有人可以提供一個解釋或參考我可以閱讀,有助於澄清這一點。

歡呼聲,

+5

http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – 2012-03-17 15:10:22

回答

3

==和===之間的差異是相當簡單:==是的值的比較。 ===是價值和類型的比較。使用===將阻止JavaScript動態確定類型,並按照原樣比較值。

5 == "5"  //true - JS compares the number 5 to a string of "5" and determines the contents are the same 
5 === "5" //false - A character is not a number, they can't be the same. 

0 == false //true - false is a bool, 0 is numerical, but JS determines that 0 evaluates to false 
0 === false //false - numeric is not boolean, they can't be exactly the same thing 

5 == 5.0  //true - need I say more? 
5 === 5.0 //true - one is a float and one is an int, but JS treats them all as numerical 

我發現它對於可以同時返回false(對於失敗)和0(作爲合法結果)的函數進行測試至關重要。

JS總共有5種基本類型=數字,字符串,布爾值,空值和未定義。 ===要求兩個參數都是相同的類型等於返回true。沒有float,int,long,short等等 - 任何類型的數字都以數字的形式集中在一起。

0

===是嚴格的相等運算符,只有當變量具有相同的類型和值時才返回true。

a = 1; 
b = "1"; 

a == b將返回true,a === b將返回false

0

==的規則是不容易記住,你可以得到下面的例子所有的正確答案?

"" == "0" // false 
0 == "" // true 
0 == "0" // true 
false == "false" // false 
false == "0" // true 
false == undefined // false 
false == null // false 
null == undefined // true 
" \t\r\n" == 0 // true 

所以最好只使用===,而從不使用==

+0

名單要大得多,我今天花了兩個小時,發現更多。 '[] ==「」','0 ==「」','「1」== true' ...等等。你也應該把源http://bonsaiden.github.com/JavaScript-Garden/ #types.equality – ajax333221 2012-05-23 02:59:40

+0

這些都是有道理的,如果你知道C.指針是整數。布爾人是整數。數組是指針。字符串是數組。 NULL是(是)0. false是0.只是說。如果你很好奇,試着找出這個問題:'[0] == false // => true' – superlukas 2015-04-24 10:40:38

1

這很簡單。
==執行類型轉換,然後將轉換後的值與期望值進行比較
===不執行類型轉換並直接比較您的值。
顯然===在性能和準確性方面更好,但在某些情況下==也很方便,所以如果它們滿足您的需求,您可以使用它們。

comparisons

乾杯!

0

你一定聽過有人說javascript是非常鬆散的類型語言。

所以如上所述超現實說 當您只想比較參數的值並且不關心它們的類型時使用(==)運算符 like 5 ==「5」將返回true。當你想看到這種情況時(例如用戶按下了什麼鍵,你不關心它的類型可能是字符串,字符或整數)。

但是我有一種情況下,參數的類型也很重要。在這種情況下,您想比較運營商的類型。所以在這些情況下,您可以使用三重相等運算符。 例如,如果您正在執行一些加法或乘法操作,那麼您要確保操作數是兼容的類型。所以你使用三重(===)運算符。

1

下面的事情,使用相等運算符

以下的全部時,你會指望一個非常完整列表是真實

0    ==   "" 
0    ==   " " 
0    ==   [] 
false   ==   "" 
false   ==   " " 
false   ==   [] 
[]    ==   "" 
null   ==   undefined 
"str"   ==   ["str"] 

"1"    ==   true 
"0"    ==   false 

"null"   !=   null 
"false"   !=   false 
"true"   !=   true 
"undefined"  !=   undefined 
"NaN"   !=   NaN 

NaN    !=   NaN   //exception: NO exception 
{}    !=   {}   //exception: same reference 
[]    !=   []   //exception: same reference 

-------------------------------------- 

new Number(10)  !==   10 
new String("str") !==   "str" 
NaN     !==   NaN  //exception: NO exception 
{}     !==   {}  //exception: same reference 
[]     !==   []  //exception: same reference 

(其中一些來自this source

總之,千萬不要使用==。甚至當你想:

「強烈建議只使用全等運算在 情況下,類型需要被強制,應當明確地做, 沒有離開語言的複雜脅迫規則「。

(source)