asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
11.9.3  The Abstract Equality Comparison Algorithm
        The comparison x == y, where x and y are values, produces true or false. Such a comparison is
        performed as follows:
       1.  If Type(x) is the same as Type(y), then
                a. If Type(x) is Undefined, return true.
                b. If Type(x) is Null, return true.
                c. If Type(x) is Number, then
                           i. If x is NaN, return false.
                          ii. If y is NaN, return false.
                         iii. If x is the same number value as y, return true.
                         iv. If x is +0 and y is 0, return true.
                          v. If x is 0 and y is +0, return true.
                         vi. Return false.
                d. 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.
                e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return
                    false.
                f. Return true if x and y refer to the same object. Otherwise, return false.
       2.  If x is null and y is undefined, return true.
       3.  If x is undefined and y is null, return true.
                                                      - 75 -
4.   If Type(x) is Number and Type(y) is String,
     return the result of the comparison x == ToNumber(y).
5. If Type(x) is String and Type(y) is Number,
     return the result of the comparison ToNumber(x) == y.
6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
8. If Type(x) is either String or Number and Type(y) is Object,
     return the result of the comparison x == ToPrimitive(y).
9. If Type(x) is Object and Type(y) is either String or Number,
     return the result of the comparison ToPrimitive(x) == y.
10. Return false.
 NOTE
 Given the above definition of equality:
 String comparison can be forced by: "" + a == "" + b.
 Numeric comparison can be forced by: a - 0 == b - 0.
 Boolean comparison can be forced by: !a == !b.
 The equality operators maintain the following invariants:
 A != B is equivalent to !(A == B).
 A == B is equivalent to B == A, except in the order of evaluation of A and B.
 The equality operator is not always transitive. For example, there might be two distinct String objects,
 each representing the same string value; each String object would be considered equal to the string
 value by the == operator, but the two String objects would not be equal to each other.
 Comparison of strings uses a simple equality test on sequences of code unit values. There is no attempt
 to use the more complex, semantically oriented definitions of character or string equality and collating
 order defined in the Unicode specification. Therefore strings that are canonically equal according to the
 Unicode standard could test as unequal. In effect this algorithm assumes that both strings are already in
 normalised form.

posted on 2010-01-26 18:13 汪杰 阅读(584) 评论(0)  编辑 收藏 引用 所属分类: javascript
只有注册用户登录后才能发表评论。

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 467490
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜