简体   繁体   English

为什么 JavaScript 没有严格的大于/小于比较运算符?

[英]Why doesn't JavaScript have strict greater/less than comparison operators?

While JavaScript's type-strict comparison operators ( === , !== ) are nice, it doesn't have corresponding strict comparisons for greater/less than.虽然 JavaScript 的类型严格比较运算符( ===!== )很好,但它没有对应的大于/小于的严格比较。

var x = 10;

x <= 20;    // true
x <= '20';    // true
x <== 20;   // true (or would be, if JS had such an operator)
x <== '20'; // false (ditto)

Why not?为什么不? I ask this question fully expecting the answer to be "uh, because it doesn't", but I'm asking anyway, in case there's an interesting and/or disheartening historical reason for such operators being omitted.我问这个问题完全期待答案是“呃,因为它没有”,但无论如何我都会问,以防这样的运算符被省略有一个有趣和/或令人沮丧的历史原因。

I can only guess-我只能猜——

If如果
a === b is false, then a === b是假的,那么
a !== b is true. a !== b是真的。 always .总是

But, this implication wouldn't hold for <==但是,这种含义对<==不成立

If如果
x <== 20 is false, we cannot infer the result of x >== 20 because it might have been false due to type check, or the relation check. x <== 20是假的,我们无法推断x >== 20的结果,因为它可能由于类型检查或关系检查而为假。

I think that's slightly confusing, although there's plenty of things in the language that are much worse (type coercion in general, to name one).我认为这有点令人困惑,尽管语言中有很多东西要糟糕得多(一般来说,类型强制,仅举一例)。

However, I think a strict < or > would behave consistently.但是,我认为严格的<>会表现一致。

由于a === btypeof a == typeof b && a == b的简写,因此您可以使用此扩展来处理不等式:例如typeof a == typeof b && a <= b

I'm not sure there is an answer to your question.我不确定您的问题是否有答案。 My guess is, the intended use is for comparing numbers to strings (and maybe booleans).我的猜测是,预期用途是将数字与字符串(可能还有布尔值)进行比较。 It actually works for those cases, as the non-strict equality operator does.它实际上适用于这些情况,就像非严格相等运算符一样。 Anything else is subject to arbitrary type coercion rules anyway.无论如何,其他任何东西都受任意类型强制规则的约束。 What would be the "correct" output of [] < {} ? [] < {}的“正确”输出是什么? false ? false Maybe undefined ?也许undefined Note that the types don't even need to be different, ({foo: 1}) < {bar : 2} also doesn't make any sense.请注意,类型甚至不需要不同, ({foo: 1}) < {bar : 2}也没有任何意义。

In my opinion, they (Brendan Eich, and later the ECMAScript committee) just decided to trust that the developers would only compare things that make sense comparing.在我看来,他们(Brendan Eich 和后来的 ECMAScript 委员会)只是决定相信开发人员只会比较有意义的东西。 Or didn't even consider that developers would try crazy comparisons.或者甚至没有考虑到开发人员会尝试疯狂的比较。 Creating extra operators for comparison would only clutter the language.为比较创建额外的运算符只会使语言混乱。 And don't forget comparisons are not the only pitfalls when dealing with type coercion, there's also addition, subtraction, etc. So I guess they just decided to be true to their decision of allowing operations between different types.并且不要忘记在处理类型强制时比较不是唯一的陷阱,还有加法、减法等。所以我猜他们只是决定忠于他们允许不同类型之间操作的决定。 They thought that would help people when they know what they're doing, but maybe didn't anticipate all the confusion that arose from that.他们认为当人们知道他们在做什么时,这会有所帮助,但也许没有预料到由此引起的所有困惑。

To show why it doesn't make sense to have it, consider instead...为了说明为什么拥有它没有意义,请考虑......

var x = 10
var less = (x <= 5)

Now, both现在,两者

x <== 5

and

x <== '5'

would be false, but for different reasons.将是错误的,但出于不同的原因。 In the first instance, you could use the assumption that x > 5, but not in the latter case.在第一种情况下,您可以使用 x > 5 的假设,但不能在后一种情况下使用。 To avoid false assumptions it is better to use === or !== first and then comparison after.为避免错误假设,最好先使用 === 或 !== ,然后再进行比较。

I'd say that the problem is that strict equality can be well defined for different types (not the same type, then not equals), but relational operators can not be well defined for different types.我想说的问题是,严格相等可以为不同的类型定义好(不是相同的类型,然后不等于),但关系运算符不能为不同的类型定义好。

Assume we define a strict comparator a <== b to be typeof a == typeof b && a <= b .假设我们将严格比较器a <== b定义为typeof a == typeof b && a <= b And the same for a >== b .对于a >== b Then we compare a = "3" and b = 3 and the results are a <== b false, a >== b false and a === b false.然后我们比较 a = "3" 和 b = 3,结果是a <== b false, a >== b false 和a === b false。 Congratulations, you just created a paradox!恭喜,你刚刚创建了一个悖论!

Such strict comparator would still mess up things like sorting algorithms, or comparing unexpected values.这种严格的比较器仍然会搞砸排序算法或比较意外值之类的事情。 For example:例如:

for (var i; i <== list.count; i++) {
  doStuff(i);
}

Note that the example is mistakenly using list.count instead of list.length , which will just return undefined , which would just return false when compared to i <== undefined , so the for loop would be entirely skipped to the surprise of the programmer.请注意,该示例错误地使用了list.count而不是list.length ,它只会返回undefined ,与i <== undefined相比,它只会返回 false ,因此 for 循环将被完全跳过,这让程序员感到惊讶.

It would me much better if JavaScript raised an error on list.count for Undefined Property, and also if comparing different types.如果 JavaScript 在list.count为未定义的属性引发错误,并且比较不同的类型,那我会好得多。

That's all I can say, comparing across types should raise an exception, just like any other decent language out there.这就是我能说的,跨类型比较应该引发异常,就像任何其他体面的语言一样。 But it doesn't.但事实并非如此。

This means, that the actual practical solution is to start using preprocessors, or just say "Oh well" and keep typing JavaScript ¯\\_(ツ)_/¯这意味着,实际可行的解决方案是开始使用预处理器,或者只是说“哦,好吧”并继续输入 JavaScript ¯\\_(ツ)_/¯

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

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