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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14533046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:53:30  来源:igfitidea点击:

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

javascript

提问by mwcz

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.

为什么不?我问这个问题完全期待答案是“呃,因为它没有”,但无论如何我都会问,以防这样的运算符被省略有一个有趣和/或令人沮丧的历史原因。

采纳答案by goat

I can only guess-

我只能猜——

If
a === bis false, then
a !== bis true. always.

如果
a === b为假,
a !== b则为真。总是

But, this implication wouldn't hold for <==

但是,这个含义不成立 <==

If
x <== 20is false, we cannot infer the result of x >== 20because it might have been false due to type check, or the relation check.

如果
x <== 20为假,我们无法推断出结果, x >== 20因为它可能由于类型检查或关系检查而为假。

I think that's slightlyconfusing, 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.

不过,我认为一个严格的人<还是>会表现得一贯的。

回答by Niet the Dark Absol

Since a === bis a shorthand for typeof a == typeof b && a == b, you can use this expansion for inequalities: typeof a == typeof b && a <= bfor example.

由于a === b是 的简写typeof a == typeof b && a == b,因此您可以将此展开式用于不等式:typeof a == typeof b && a <= b例如。

回答by bfavaretto

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? Maybe undefined? Note that the types don't even need to be different, ({foo: 1}) < {bar : 2}also doesn't make any sense.

我不确定您的问题是否有答案。我的猜测是,预期用途是将数字与字符串(可能还有布尔值)进行比较。它实际上适用于这些情况,就像非严格相等运算符一样。无论如何,其他任何东西都受任意类型强制规则的约束。什么是“正确”的输出[] < {}false? 也许undefined?请注意,类型甚至不需要不同,({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. 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.

在我看来,他们(Brendan Eich 和后来的 ECMAScript 委员会)只是决定相信开发人员只会比较有意义的东西。或者甚至没有考虑到开发人员会尝试疯狂的比较。为比较创建额外的运算符只会使语言变得混乱。并且不要忘记在处理类型强制时比较不是唯一的陷阱,还有加法、减法等。所以我猜他们只是决定忠于他们允许不同类型之间进行操作的决定。他们认为当人们知道他们在做什么时,这会有所帮助,但也许没有预料到由此引起的所有困惑。

回答by Tor Iver Wilhelmsen

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. To avoid false assumptions it is better to use === or !== first and then comparison after.

将是错误的,但出于不同的原因。在第一种情况下,您可以使用 x > 5 的假设,但不能在后一种情况下使用。为避免错误假设,最好先使用 === 或 !== ,然后再进行比较。

回答by tothemario

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 <== bto be typeof a == typeof b && a <= b. And the same for a >== b. Then we compare a = "3" and b = 3 and the results are a <== bfalse, a >== bfalse and a === bfalse. Congratulations, you just created a paradox!

假设我们将严格比较器定义a <== btypeof a == typeof b && a <= b。对于a >== b. 然后我们比较a = "3"和b = 3,结果是a <== b假的,a >== b假的和a === b假的。恭喜,你刚刚创建了一个悖论!

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.countinstead 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,与 相比只会返回 false i <== undefined,因此 for 循环将被完全跳过,这让程序员感到惊讶。

It would me much better if JavaScript raised an error on list.countfor Undefined Property, and also if comparing different types.

如果 JavaScriptlist.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 ˉ\_(ツ)_/ˉ