在Javascript中,< int-value> =="< int-value>计算结果为true。为什么会这样? [英] In Javascript, <int-value> == "<int-value>" evaluates to true. Why is it so?
问题描述
如果我做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:
- 如果Type(x) ,请转到步骤14.
- 如果Type(x)为Undefined,则返回true。
- 如果Type(x)为Null,
- 如果类型(x)不是数字,请转到步骤11.
- 如果x是NaN,返回false。 $ b
- 如果y是NaN,则返回false。
- 如果x与y的数字值相同,则返回true。
- 如果x是-0,y是+0,则返回true。
- 返回false
- 如果Type(x)是String,则返回true,如果x和y完全相同
字符序列(相同长度
和相应字符
位置)。 - 如果Type(x)为Boolean,如果x和y都为true或两者都为
false,则返回true。 - 如果x和y指向同一个对象,或者如果它们引用
对象(参见13.1.2),则返回true。 - 如果x未定义且y为null,则返回true。如果x未定义,则返回true。
- 如果Type(x)是Number,Type(y)是String,则返回
比较x == ToNumber(y)的结果。 b $ b - 如果Type(x)是String并且Type(y)是Number,则返回
比较结果ToNumber(x)== y。 - 如果Type(x)是Boolean,则返回比较结果ToNumber(x)== y。
-
ToNumber(y)。 - 如果Type(x)是String或Number,Type(y)是Object,返回
比较结果x = =
ToPrimitive(y)。 - 如果Type(x)是Object,类型(y)是String或Number,返回
比较结果
ToPrimitive(x)== y。 - 返回false。
- If Type(x) is different from Type(y), go to step 14.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is not Number, go to step 11.
- If x is NaN, return false.
- If y is NaN, return false.
- If x is the same number value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- 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.
- If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
- 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.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
- If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
- If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
- 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屋!