Explain the comparison in javascript in detail

Code 1: 

  1. [] == [];  // false  
  2. [] === []; // false  
  3. {} == {};  // SyntaxError: Unexpected token ==  
  4. {} === {}; // SyntaxError: Unexpected token ===  

Code 2:

  1. var  n0 = 123;  
  2. var n1 = new Number(123);  
  3. var n2 = new Number(123);  
  4. var  n3 = Number (123);  
  5. var  n4 = Number (123);  
  6.   
  7. n0 == n1;  // true  
  8. n0 == n3;  // true  
  9. n0 === n1; // false  
  10. n0 === n3; // true  
  11.   
  12. n1 == n2;  // false  
  13. n1 === n2; // false  
  14. n1 == n3;  // true  
  15. n1 === n3; // false  
  16.   
  17. n3 == n4;  // true  
  18. n3 === n4; // true  

 Why is this result? 

We need to read the specification carefully:  http://ecmascript.cn/#203

11.9.3 Abstract equality comparison algorithm

Comparison operation  x==y, where the  x sum  y is the value, produces an  true OR  false. Such comparisons are made as follows:

  1. If it  Type(x) is the  Type(y) same, then
    1. If  Type(x) to  Undefinedreturn  true.
    2. If  Type(x) to  Nullreturn  true.
    3. If  Type(x) is  Number, then
      1. If  x to  NaNreturn  false.
      2. If  y to  NaNreturn  false.
      3. If  x and  y are equal values, return  true.
      4. If  x it is  +0 and  y is  −0to return  true.
      5. If  x it is  −0 and  y is  +0to return  true.
      6. Return  false.
    4. If it  Type(x) is  String, it  returns when the  x and  yis the same character sequence (the same length and the same character in the same position)  true. Otherwise, return  false.
    5. 若 Type(x) 为 Boolean, 当 x 和 y 为同为 true 或者同为 false 时返回 true。 否则, 返回false
    6. 当 x 和 y 为引用同一对象时返回 true。否则,返回 false
  2. 若 x 为 null 且 y 为 undefined, 返回 true
  3. 若 x 为 undefined 且 y 为 null, 返回 true
  4. 若 Type(x) 为 Number 且 Type(y) 为 String, 返回 comparison x == ToNumber(y) 的结果。
  5. 若 Type(x) 为 String 且 Type(y) 为 Number
  6. 返回比较 ToNumber(x) == y 的结果。
  7. 若 Type(x) 为 Boolean, 返回比较 ToNumber(x) == y 的结果。
  8. 若 Type(y) 为 Boolean, 返回比较 x == ToNumber(y) 的结果。
  9. 若 Type(x) 为 String 或 Number,且 Type(y) 为 Object,返回比较 x == ToPrimitive(y) 的结果。
  10. 若 Type(x) 为 Object 且 Type(y) 为 String 或 Number, 返回比较 ToPrimitive(x) == y 的结果。
  11. 返回 false

注:按以上相等之定义:

  • 字符串比较可以按这种方式强制执行: "" + a == "" + b
  • 数值比较可以按这种方式强制执行: +a == +b
  • 布尔值比较可以按这种方式强制执行: !a == !b

注:等值比较操作保证以下不变:

  • A != B 等价于 !(A==B)
  • A == B 等价于 B == A,除了 A 与 B 的执行顺序。

注:相等运算符不总是传递的。 例如,两个不同的 String 对象,都表示相同的字符串值;== 运算符认为每个 String 对象都与字符串值相等,但是两个字符串对象互不相等。例如:

  • new String("a") == "a" 和 "a" == new String("a") 皆为 true
  • new String("a") == new String("a") 为 false

字符串比较使用的方式是简单地检测字符编码单元序列是否相同。不会做更复杂的、基于语义的字符或者字符串相等的定义以及 Unicode 规范中定义的 collating order。所以 Unicode 标准中认为相等的 String 值可能被检测为不等。实际上这一算法认为两个字符串已经是经过规范化的形式。

11.9.6 严格等于比较算法

比较 x===yx 和 y 为值,需要产出 true 或 false。比较过程如下:

  1. 如果 Type(x) 与 Type(y) 的结果不一致,返回 false,否则
  2. 如果 Type(x) 结果为 Undefined,返回 true
  3. 如果 Type(x) 结果为 Null,返回 true
  4. 如果 Type(x) 结果为 Number,则
    1. 如果 x 为 NaN,返回 false
    2. 如果 y 为 NaN,返回 false
    3. 如果 x 与 y 为同一个数字,返回 true
    4. 如果 x 为 +0y 为 -0,返回 true
    5. 如果 x 为 -0y 为 +0,返回 true
    6. 返回 false
  5. 如果 Type(x) 结果为 String,如果 x 与 y 为完全相同的字符序列(相同的长度和相同的字符对应相同的位置),返回 true,否则,返回 false
  6. 如果 Type(x) 结果为 Boolean,如果 x 与 y 都为 true 或 false,则返回 true,否则,返回false
  7. 如果 x 和 y 引用到同一个 Object 对象,返回 true,否则,返回 false

注:此算法与 SameValue 算法在对待有符号的零和 NaN 上表现不同。

Guess you like

Origin blog.csdn.net/justjavac/article/details/11920299