【问题标题】:equality in javascript [duplicate]javascript中的平等[重复]
【发布时间】:2012-05-23 11:08:29
【问题描述】:

在使用 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

谁能提供我能读到的解释或参考资料,以帮助澄清这一点。

干杯,

【问题讨论】:

标签: javascript


【解决方案1】:

== 和 === 之间的区别相当简单:== 是值的比较。 === 是值和类型的比较。使用 === 将阻止 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 种基本类型 = 数字、字符串、布尔值、null 和未定义。 === 要求两个参数的类型相同并且 值相等才能返回 true。没有浮点数、整数、长整数、短整数等 - 任何类型的数字都被归为数字。

【讨论】:

    【解决方案2】:

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

    comparisons

    干杯!

    【讨论】:

      【解决方案3】:

      下面是一个非常完整的列表,列出了使用相等运算符时你不会想到的事情

      以下都是正确的

      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)

      【讨论】:

        【解决方案4】:

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

        a = 1;
        b = "1";
        

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

        【讨论】:

          【解决方案5】:

          ==的规则不好记,下面的例子你能猜对吗?

          "" == "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 == " ", "1" == true... 等等。另外你应该把来源bonsaiden.github.com/JavaScript-Garden/#types.equality
          • 如果你知道 C,这些都是有意义的。指针是整数。布尔值是整数。数组是指针。字符串是数组。 NULL 是(曾经)0。false 是 0。只是说。如果您好奇,请尝试弄清楚这个是怎么回事:[0] == false // =&gt; true
          【解决方案6】:

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

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

          但在我看来,论点的类型也很重要。在这种情况下,您需要比较运算符的类型。因此,在这些情况下,您使用三等式运算符。 例如,如果您正在执行一些加法或乘法运算,那么您要确保操作数是兼容的类型。因此,您使用三元组 (===) 运算符。

          【讨论】:

            猜你喜欢
            • 2016-06-24
            • 1970-01-01
            • 2011-11-20
            • 1970-01-01
            • 1970-01-01
            • 2018-12-19
            • 2015-08-10
            • 2017-06-19
            • 1970-01-01
            相关资源
            最近更新 更多