简体   繁体   English

Javascript平等三等于但是大于和小于?

[英]Javascript equality triple equals but what about greater than and less than?

I was explaining to a colleague that you should use === and !== (and >== and <== of course) when comparing variables in JavaScript so that it doesn't coerce the arguments and get all froopy and confusing but they asked me a two part question that I did not know the answer to and thought I would ask the experts here, specifically it is: 我正在向一位同事解释,在比较JavaScript中的变量时,你应该使用===!== (以及>==<==当然),这样它就不会强迫参数,并且会让所有的事情变得困难而且令人困惑但是他们问我一个两部分问题,我不知道答案,并认为我会问这里的专家,具体是:

What about > and < - when they compare do they also coerce the arguments or not - why isn't there some sort of >> and << operator (probably need to be some other syntax as I would guess they would be bit shift operators if it is going along the whole C style but you get the gist)? 那么>< - 当他们比较时他们是否也强制参数或者 - 为什么不存在某种>><<运算符(可能需要一些其他语法,因为我猜他们会是位移运算符如果它是沿着整个C风格,但你得到的要点)?

So I can write a test to find the answer to the first part, which I did, here it is: 所以我可以写一个测试来找到第一部分的答案,我做了,这里是:

// Demo the difference between == and ===
alert(5 == "5");
alert(5 === "5");   

// Check out what happens with >
alert(5 > "4");    
alert(5 > 4);

and it returned: 它返回了:

true
false

true
true

so it does look like the > is doing the coercion since > "4" and > 4 return the same result. 所以它看起来像是>正在进行强制,因为> "4"> 4返回相同的结果。 so how about the second part... 那么第二部分怎么样......

Is there some sort of operator for > and < that do not coerce the type (or how can I change my test to perform the test safely)? 是否有某种类型的运算符><不强制类型(或者如何更改我的测试以安全地执行测试)?

No, there's no need for such operators. 不,没有必要这样的运营商。 The type checking done for those relational operators is different than for equality and inequality. 为那些关系运算符进行的类型检查不同于对于相等和不平等的类型检查。 ( edit — perhaps it's a little strong to say that there's "no need"; that's true only because JavaScript deems it so :-) 编辑 - 或许它说“没有必要”有点强烈;这只是因为JavaScript认为如此:-)

Specifically, the > and < and >= and <= operators all operate either on two numeric values, or two strings, preferring numeric values. 具体来说, ><>=<=运算符都可以在两个数值或两个字符串上运行,而不是数值。 That is, if one value is a number, then the other is treated as a number. 也就是说,如果一个值是数字,则另一个值被视为数字。 If a non-number can't be cleanly converted to a number (that is, if it ends up as NaN ), then the result of the comparison is undefined . 如果无法将非数字干净地转换为数字(即,如果它最终为NaN ),则比较的结果是undefined (That's a little problematic, because undefined will look like false in the context of an if statement.) (这有点问题,因为在if语句的上下文中, undefined看起来像false 。)

If both values are strings, then a collating-order string comparison is performed instead. 如果两个值都是字符串,则执行整理顺序字符串比较。

If you think about it, these comparisons don't make any sense for object instances; 如果你考虑一下,这些比较对于对象实例没有任何意义; what does it mean for an object to be "greater than" another? 对象“大于”另一个对象意味着什么? I suppose, perhaps, that this means that if you're finding yourself with values of variant types being compared like this, and that's causing problems, then yes you have to detect the situation yourself. 我想,也许,这意味着,如果你发现自己的变体类型的值被比作这样,并且这会导致问题,那么是的,你必须自己检测这种情况。 It seems to me that it would be good to work upstream and think about whether there's something fishy about the code that's leading to such a situation. 在我看来,在上游工作并考虑是否存在导致这种情况的代码有些可疑的事情会很好。

Is there some sort of operator for > and < that do not coerce the type 是否有某种运算符>和<不强制类型

No. 没有。

how can I change my test to perform the test safely 如何更改测试以安全地执行测试

You would have to explicitly test the types: 您必须明确测试类型:

typeof a === typeof b && a > b

I referenced Flanagan's JavaScript: The Definitive Guide (5th Ed.) and there does not seem to be non-coercive comparison operators. 我引用了Flanagan的JavaScript:The Definitive Guide(第5版),似乎没有非强制性的比较运算符。

You are right in saying the << and >> are indeed bitwise operators so that wouldn't work. 你是对的说<<和>>确实是按位运算符,所以这是行不通的。

I would suggest you deliberately coerce the values youself: 我建议你故意强迫自己的价值观:

var num_as_string = '4';
var num = +num_as_string;
if (5 > num) { ... }
12 > '100' // false
'12' > 100 // false
'12' > '100' // true

As others mentioned, if one is a number the other is casted to a number. 正如其他人所提到的,如果一个是数字,另一个是数字。 Same rule applies to these cases as well: 同样的规则也适用于这些情况:

null > 0 // false
null < 0 // false
null >= 0 // true

However, there might be cases that you would need null >= 0 to give false (or any of the number string comparison cases above), therefore it is indeed a need to have strict comparison >== or <== . 但是,可能存在需要null >= 0来给出false (或上面的任何数字字符串比较情况)的情况,因此确实需要进行严格比较>==<==

For example, I am writing a compareFunction for the Array.prototype.sort() and an expression like x>=0 would treat null values like 0's and put them together, whereas I want to put them elsewhere. 例如,我正在为Array.prototype.sort()编写一个compareFunction ,并且像x>=0这样的表达式会将null值视为0并将它们放在一起,而我想把它们放在别处。 I have to write extra logic for those cases. 我必须为这些案件编写额外的逻辑。

Javascript says deal with it on your own (in practice). Javascript说你自己处理它(在实践中)。

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

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