js是如何进行比较运算的
2017-09-10 23:53
375 查看
一个有趣的事实:
首先第一条,为什么
js是如何进行== 操作的? 参考这个
如果按照这个流程下来,我们直接从第1条跳到第14条,然后发现没有符合的条件,所以直接返回false..
第二条,为什么
js是如何进行
其中ToPrimitive按照以下规则。
ToNumber按照以下规则
如此走下来的话我们会在第8条终止,因为+0 == 0,所以返回false。
接下来,为什么
js中并未对此类运算符有明确的计算规则,但是它会按照以下规则执行:
好像并没什么问题。。
因为
以前我是认为事物是非true即false的,后来数据库老师说过在它们之间还有一个unknown,类似薛定谔的猫,非死非活,或者说既死也活。
这样看来,其实计算机的判断哲学还是更好理解些吧。
原文链接
首先第一条,为什么
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 Type | Result |
---|---|
Undefined | No Conversion |
Null | No Conversion |
Boolean | No Conversion |
Number | No Conversion |
String | No Conversion |
Object | Return 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 |
Input Type | Result |
---|---|
Undefined | NaN |
Null | +0 |
Number | No Conversion |
Boolean | The result is 1 if the argument is true. The result is +0 if the argument is false. |
… | … |
接下来,为什么
null >= 0为true。
js中并未对此类运算符有明确的计算规则,但是它会按照以下规则执行:
if null < 0 is false, then null >= 0 is true
好像并没什么问题。。
因为
null < 0为false,所以它的对立面
null >= 0就为true。
以前我是认为事物是非true即false的,后来数据库老师说过在它们之间还有一个unknown,类似薛定谔的猫,非死非活,或者说既死也活。
这样看来,其实计算机的判断哲学还是更好理解些吧。
原文链接
相关文章推荐
- [将小白进行到底] 如何比较两篇文章的相似度
- c#如何进行时间值的比较
- js下如何弹出保存对话框进行文件下载
- 计算机IP地址与子网掩码如何进行AND运算
- java内嵌chrome浏览器,JS和JAVA如何进行交互
- (原创)如何进行有符号小数乘法运算?(Verilog)
- 用Beyond Compare如何进行文本比较
- shell中如何进行算术运算
- 可以在js中进行加减乘除运算吗?
- 计算机IP地址与子网掩码如何进行AND运算
- 脑弄大开之——如何抛弃数字和运算符号只用计算机和函数进行运算
- asp两个数组如何进行比较,剔除相同,保留不同
- 混合运算中,隐式转换是如何进行的?
- Android 如何进行js交互
- js下如何弹出保存对话框进行文件下载
- 用 sco.js valid 插件进行表单验证时,表单是如何提交的
- 如何使用jquery对特殊字符进行转义,防止js注入
- python如何让类支持比较运算
- 如何进行js的跨域数据传输?
- js:数组重排序问题:如何使用sort()方法按数值的大小进行升序或降序排列