在Javascript中,< int-value> =="< int-value>计算结果为true。为什么会这样? [英] In Javascript, <int-value> == "<int-value>" evaluates to true. Why is it so?

查看:115
本文介绍了在Javascript中,< int-value> =="< int-value>计算结果为true。为什么会这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做0 ==0,它的值为true。尝试,

If I do 0 == "0" it evaluates to true. Try,

if( -777 == "-777" ) alert("same");

警报。

也注意到true ==true不评估为true。尝试,

And, it's also noticeable that true == "true" doesn't evaluate to true. Try,

if( false == "false" ) alert("same");

警报不会发生。

为什么会这样?

推荐答案

==的行为有点长,但在 ecma-262规范

The behavior of == is a bit lengthy, but clearly defined in the ecma-262 spec:


11.9.3抽象等式比较算法

11.9.3 The Abstract Equality Comparison Algorithm

比较x == y,其中x和y
是值,产生true或false。
这样的比较执行为
如下:

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:


  1. 如果Type(x) ,请转到步骤14.

  2. 如果Type(x)为Undefined,则返回true。

  3. 如果Type(x)为Null,

  4. 如果类型(x)不是数字,请转到步骤11.

  5. 如果x是NaN,返回false。 $ b
  6. 如果y是NaN,则返回false。

  7. 如果x与y的数字值相同,则返回true。

  8. 如果x是-0,y是+0,则返回true。

  9. 返回false

  10. 如果Type(x)是String,则返回true,如果x和y完全相同
    字符序列(相同长度
    和相应字符
    位置)。

  11. 如果Type(x)为Boolean,如果x和y都为true或两者都为
    false,则返回true。

  12. 如果x和y指向同一个对象,或者如果它们引用
    对象(参见13.1.2),则返回true。

  13. 如果x未定义且y为null,则返回true。如果x未定义,则返回true。

  14. 如果Type(x)是Number,Type(y)是String,则返回
    比较x == ToNumber(y)的结果。 b $ b
  15. 如果Type(x)是String并且Type(y)是Number,则返回
    比较结果ToNumber(x)== y。

  16. 如果Type(x)是Boolean,则返回比较结果ToNumber(x)== y。


  17. ToNumber(y)。

  18. 如果Type(x)是String或Number,Type(y)是Object,返回
    比较结果x = =
    ToPrimitive(y)。

  19. 如果Type(x)是Object,类型(y)是String或Number,返回
    比较结果
    ToPrimitive(x)== y。

  20. 返回false。

  1. If Type(x) is different from Type(y), go to step 14.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is not Number, go to step 11.
  5. If x is NaN, return false.
  6. If y is NaN, return false.
  7. If x is the same number value as y, return true.
  8. If x is +0 and y is −0, return true.
  9. If x is −0 and y is +0, return true.
  10. Return false.
  11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
  12. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
  13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.
  14. If x is null and y is undefined, return true.
  15. If x is undefined and y is null, return true.
  16. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  17. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  18. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  20. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  21. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
  22. Return false.



b $ b

步骤16适用于您之前的示例:

Step 16 applies to your former example:

   0 == "0"            // apply 16
≡  0 == toNumber("0")
≡  0 == 0              // apply 7
≡  true

并且步骤18,然后步骤16,适用于后者:

And step 18, then step 16, apply to the latter:

   true == "true"            // apply 18
≡  toNumber(true) == "true"
≡  1 == "true"               // apply 16
≡  1 == toNumber("true")
≡  1 == NaN                  // apply 6
≡  false

这篇关于在Javascript中,< int-value> =="< int-value>计算结果为true。为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆