是否支持Javascript链式关系运算符? [英] Javascript chained relational operators supported?

查看:47
本文介绍了是否支持Javascript链式关系运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚研究了一些JS核心原理,发现该引擎在评估链式关系运算符时不会抛出错误.相反,它们以我自己无法理解的方式进行评估.

I just played around with some JS core principles and found, that the engine evaluates chained relational operators without throwing an error. Instead they are evaluated in a way I can't understand for myself.

console.log(1 < 2 < 3 < 4 < 5); //true, expected
console.log(5 > 4 > 3 > 2 > 1); //false, should be true
console.log(5 >= 4 >= 3);       //false, should be true
console.log(7 >= -2 >= 1);      //true, should be false
console.log(1 <= -2 <= 7);      //true, should be false

这甚至得到正式支持吗?我也没有在文献/文档中提到这一点,我很困惑为什么这甚至行得通.

Is this even officially supported? I also found no mentions in literature / documentations on this and I am rather confused why this is even working.

有人可以点亮一点吗?

推荐答案

它们是二进制运算符,具有左关联性.他们被解析为

They're binary operators, with left associativity. They're parsed as

console.log((((1 < 2) < 3) < 4) < 5); // true (true < 5)
console.log((((5 > 4) > 3) > 2) > 1); // false (true > 1)
console.log((5 >= 4) >= 3);           // false (true >= 3)
console.log((7 >= -2) >= 1);          // true (true >= 1)
console.log((1 <= -2) <= 7);          // true (false <= 7)

,并将布尔部分结果与数字进行比较.

and will compare boolean partial results with numbers.

这篇关于是否支持Javascript链式关系运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆