简体   繁体   English

为什么 (0 < 5 < 3) 返回真?

[英]Why does (0 < 5 < 3) return true?

I was playing around in jsfiddle.net and I'm curious as to why this returns true?我在 jsfiddle.net 上玩,我很好奇为什么这会返回 true?

if(0 < 5 < 3) {
    alert("True");
}

So does this:这样做也是如此:

if(0 < 5 < 2) {
    alert("True");
}

But this doesn't:但这不是:

if(0 < 5 < 1) {
    alert("True");
}

Is this quirk ever useful?这个怪癖有用吗?

Order of operations causes (0 < 5 < 3) to be interpreted in javascript as ((0 < 5) < 3) which produces (true < 3) and true is counted as 1, causing it to return true.操作顺序导致(0 < 5 < 3)在 javascript 中被解释为((0 < 5) < 3)产生(true < 3)并且 true 被计为 1,导致它返回 true。

This is also why (0 < 5 < 1) returns false, (0 < 5) returns true, which is interpreted as 1 , resulting in (1 < 1) .这也是为什么(0 < 5 < 1)返回 false, (0 < 5)返回 true,这被解释为1 ,导致(1 < 1)

我的猜测是因为0 < 5为真,而true < 3被转换为1 < 3 ,这是真的。

可能是因为true被假定为1所以

0 < 5 < 3  -->  true < 3 -->  1 < 3  --> true

因为true < 3 ,因为true == 1

As to your question whether this quirk is ever useful: I suppose there could be some case where it would useful (if condensed code is what you are after), but relying on it will (most likely) severely reduce the understandability of your code.至于你这个怪癖是否有用的问题:我想在某些情况下它可能会有用(如果你所追求的是浓缩代码),但依赖它(很可能)会严重降低你的代码的可理解性。

It's kind of like using post/pre increment/decrement as a part of bigger expressions.这有点像使用 post/pre increment/decrement 作为更大表达式的一部分。 Can you determine what this code's result is at a glance?你能一眼就判断出这段代码的结果是什么吗?

int x = 5;
int result = ++x + x++ + --x;

Note: with this code, you can sometimes even get different results depending on the language and compiler.注意:使用此代码,有时甚至可以根据语言和编译器获得不同的结果。

It's a good idea to make life easy for yourself and the next guy who will read your code.让自己和下一个将阅读您的代码的人的生活变得轻松是个好主意。 Clearly write out what you actually want to have happen rather then relying on side effects like the implicit conversion of booleans.清楚地写出您真正想要发生的事情,而不是依赖于诸如布尔值的隐式转换之类的副作用。

The answer to the second part of the question, "is this quirk ever useful?"问题第二部分的答案,“这个怪癖有用吗?” is perhaps no, as noted by a previous answer, if it is indeed a quirk of the language (Javascript) that true is cast to 1, but that the programmer does not in general view 1 and true (and 0 and false) as the same thing.也许不是,正如之前的答案所指出的那样,如果 true 被强制转换为 1 确实是语言 (Javascript) 的一个怪癖,但程序员通常不会将 1 和 true(以及 0 和 false)视为同样的事情。

If however you have a mental model of 1 being true and 0 being false, then it leads to all sorts of nice boolean techniques that are extremely useful, powerful, and direct.然而,如果你有一个 1 为真而 0 为假的心理模型,那么它会导致各种非常有用、强大和直接的很好的布尔技术。 For example, you could increment a counter directly with the result of A > 100, which would increment the counter if A is greater than 100. This technique might be viewed as a quirk or a trick in Java, but in an array or functional language may be idiomatic.例如,您可以直接使用 A > 100 的结果递增计数器,如果 A 大于 100,则计数器递增。此技术在 Java 中可能被视为怪癖或技巧,但在数组或函数式语言中可能是惯用的。

A classic example in the array language APL would be to count the number of items in an array that are (say) greater than 100:数组语言 APL 中的一个经典示例是计算数组中(例如)大于 100 的项目数:

+/A>100

Where if A is the 5 item array 107 22 256 110 3 then:如果 A 是 5 项数组 107 22 256 110 3 那么:

A>100

yields the 5 item boolean array:产生 5 项布尔数组:

1 0 1 1 0 1 0 1 1 0

and summing this boolean result:并总结这个布尔结果:

+/1 0 1 1 0

yields the final answer:得出最终答案:

3 3

This question is a perfect example of where this technique would be very useful, especially if the problem is generalized to determine if n out of m boolean values are true. 这个问题是这个技术非常有用的一个完美例子,特别是如果问题被概括为确定 m 个布尔值中的 n 个是否为真。

Check if at least two out of three booleans are true 检查三个布尔值中是否至少有两个为真

That's easy.这很容易。

(0 < 5 < 3)

Start with left to right so it evaluates the first 0 < 5. Is it true?从左到右开始,所以它评估第一个 0 < 5。这是真的吗? Yes.是的。 Since TRUE=1, it evaluates 1 < 3. Since 1 is less than 3 so it's true.由于 TRUE=1,它计算 1 < 3。由于 1 小于 3,所以它是真的。

Now with this现在有了这个

 (0 < 5 < 1)

Is 0 less than 5? 0 小于 5 吗? Yes.是的。 So make it TRUE which also means 1. Now with that fact in mind, it evaluates to (1 < 1).所以让它为 TRUE,这也意味着 1。现在考虑到这个事实,它的计算结果为 (1 < 1)。 Is 1 less than 1? 1 小于 1 吗? No, therefore it's false.不,所以它是假的。 It has to be equal.它必须是平等的。

除了 python,CoffeeScript 是另一种支持链式比较的语言,因此3 < x < 10在 vanilla JS 中将被转换为(3 < x && x < 10)

is it evaluating 0<5 which would return 1 for true when 1<3 which is true?是否评估 0<5 当 1<3 为真时返回 1 为真?

C# want let you do this "Operator '<' cannot be applied to operands of type 'bool' and 'int'" C#想让你这样做“运算符'<'不能应用于'bool'和'int'类型的操作数”

I ran into this a little while ago in Obj-C and was very puzzled by it.不久前我在 Obj-C 中遇到了这个问题,并对此感到非常困惑。 I got the results I wanted by doing something like this:我通过做这样的事情得到了我想要的结果:

if(0 < 5  && 5 < 3) {
alert("True");}

Which of course is false so you wouldn't get that "true" alert.这当然是错误的,所以你不会得到那个“真实”的警报。 Glad I read this, I now know why.很高兴我读到了这个,我现在知道为什么了。

0 < 5 < 3 
==> ( ( 0 < 5 ) < 3 )
==> true < 3
==> 1 < 3
==> true

因为 0 小于 5 然后返回 true,默认情况下 true 是任何包括并且可以评估为 1 仍然小于 3 再次返回 true

A boolean operand when operated over a math operator returns a number.布尔操作数在对数学运算符进行运算时返回一个数字。 to check this we do检查这个我们做

true + 1  which gives you 2.

So 0 < 5 , the returned boolean(true) operated with math operator(<) will return a number.所以0 < 5 ,返回的 boolean(true) 用数学 operator(<) 操作将返回一个数字。 So it boils to 1<3 which returns true所以它归结为 1<3 返回true

try phrasing your results as Number()尝试将结果表述为 Number()

if(Number(0) < Number(5) < Number(3)) {
    alert("True");
}

or try this:或者试试这个:

if(Number(0) < Number(5) && Number(5) < Number(3)) {
    alert("True");
}

I googled this because I was getting (3 >= 20) //returning true and I guess javascript was trying to check 3 as a boolean because I was getting this value from the elm.getAttribute();我用elm.getAttribute();搜索这个是因为我得到(3 >= 20) //returning true并且我猜 javascript 试图检查3作为一个布尔值,因为我从elm.getAttribute();获取这个值elm.getAttribute(); function which console.log();函数 which console.log(); was printing in String form.以字符串形式打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM