javascript中的平等

时间:2012-03-17 15:07:56

标签: 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

有人可以提供我可以阅读的解释或参考,以帮助澄清这一点。

欢呼声,

6 个答案:

答案 0 :(得分:4)

==和===之间的区别非常简单:==是值的比较。 ===是价值和类型的比较。使用===将阻止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种基本类型= numeric,string,boolean,null和undefined。 ===要求两个参数具有相同的类型相等的值才能返回true。没有float,int,long,short等等 - 任何类型的数字都被归为一个数字。

答案 1 :(得分:1)

很简单。
==执行类型转换,然后将转换后的值与期望值进行比较
===不执行类型转换并直接比较您的值 显然===在性能和准确性方面更好,但在某些情况下==也很方便,因此如果它们符合您的需要,您可以使用它们。

comparisons

干杯!

答案 2 :(得分: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)

答案 3 :(得分:0)

===是严格相等运算符,仅当变量具有相同的类型和值时才返回true。

a = 1;
b = "1";

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

答案 4 :(得分: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

因此最好只使用===,而不要使用==

答案 5 :(得分:0)

你一定听过有人说javascript是一种非常松散的语言。

正如上面所说的超现实主义 当您只想比较参数的值而不关心它们的类型时,使用(==)运算符 像5 ==“5”将返回true。当你想看时(例如用户按下了什么键而你不关心它的类型可能是字符串,字符或整数)就是这种情况。

但是我认为论证的类型也很重要。在这种情况下,您希望比较运算符的类型。因此,在这些情况下,您使用三重相等运算符。 例如,如果要执行一些加法或乘法运算,则需要确保操作数是兼容类型。所以你使用triple(===)运算符。