您的位置:首页 > Web前端 > JavaScript

js是如何进行比较运算的

2017-09-10 23:53 375 查看
一个有趣的事实:



首先第一条,为什么
null == 0
为false。


js是如何进行== 操作的? 参考这个

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.


如果按照这个流程下来,我们直接从第1条跳到第14条,然后发现没有符合的条件,所以直接返回false..

第二条,为什么
null > 0
为false.


js是如何进行
>
<
操作的? 参考这个

1. Call ToPrimitive(x, hint Number).
2. Call ToPrimitive(y, hint Number).
3. If Type(Result(1)) is String and Type(Result(2)) is String, go to step 16. (Note that this step differs from step 7 in the algorithm for the addition operator + in using 'and' instead of 'or'.)
4. Call ToNumber(Result(1)).
5. Call ToNumber(Result(2)).
6. If Result(4) is NaN, return undefined.
7. If Result(5) is NaN, return undefined.
8. If Result(4) and Result(5) are the same number value, return false.
9. If Result(4) is +0 and Result(5) is -0, return false.
10. If Result(4) is -0 and Result(5) is +0, return false.
11. If Result(4) is +∞, return false.
12. If Result(5) is +∞, return true.
13. If Result(5) is -∞, return false.
14. If Result(4) is -∞, return true.
15. If the mathematical value of Result(4) is less than the mathematical value of Result(5) --- note that these mathematical values are both finite and not both zero --- return true. Otherwise, return false.
16. If Result(2) is a prefix of Result(1), return false. (A string value p is a prefix of string value q if q can be the result of concatenating p and some other string r. Note that any string is a prefix of itself, because r may be the empty string.)
17. If Result(1) is a prefix of Result(2), return true.
18. Let k be the smallest nonnegative integer such that the character at position k within Result(1) is different from the character at position k within Result(2). (There must be such a k, for neither string is a prefix of the other.)
19. Let m be the integer that is the code point value for the character at position k within Result(1).
20. Let n be the integer that is the code point value for the character at position k within Result(2).
21. If m < n, return true. Otherwise, return false.


其中ToPrimitive按照以下规则。

Input TypeResult
UndefinedNo Conversion
NullNo Conversion
BooleanNo Conversion
NumberNo Conversion
StringNo Conversion
ObjectReturn a default value for the Object. The default value of an object is retrieved by calling the internal [[DefaultValue]] method of the object, passing the optional hint PreferredType
ToNumber按照以下规则

Input TypeResult
UndefinedNaN
Null+0
NumberNo Conversion
BooleanThe result is 1 if the argument is true. The result is +0 if the argument is false.
如此走下来的话我们会在第8条终止,因为+0 == 0,所以返回false。

接下来,为什么
null >= 0
为true。


js中并未对此类运算符有明确的计算规则,但是它会按照以下规则执行:

if null < 0 is false, then null >= 0 is true


好像并没什么问题。。

因为
null < 0
为false,所以它的对立面
null >= 0
就为true。

以前我是认为事物是非true即false的,后来数据库老师说过在它们之间还有一个unknown,类似薛定谔的猫,非死非活,或者说既死也活。

这样看来,其实计算机的判断哲学还是更好理解些吧。

原文链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: