为什么JavaScript没有严格大于/小于比较运算符? [英] Why doesn't JavaScript have strict greater/less than comparison operators?
问题描述
虽然JavaScript的类型严格比较运算符( ===
,!==
)很好,但它没有没有相应的严格比较大于/小于。
While JavaScript's type-strict comparison operators (===
, !==
) are nice, it doesn't have corresponding strict comparisons for greater/less than.
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.
推荐答案
我只能猜测 -
如果
a === b
为假,然后
a!== b
为真。的 总是 即可。
If
a === b
is false, then
a !== b
is true. always.
但是,这种含义不适用于< ==
But, this implication wouldn't hold for <==
如果
x< == 20
为false,我们无法推断
<$ c $的结果c> x> == 20 因为类型检查或关系检查可能是假的。
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.
我认为这是<虽然语言中有很多东西要糟糕得多(一般来说类型强制,但要说出一个)。
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.
这篇关于为什么JavaScript没有严格大于/小于比较运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!