JavaScript中的比较运算符是否存在类型安全的等价物?

时间:2021-07-28 16:01:48

I just tried the following in a node.js console:

我刚刚在node.js控制台中尝试了以下内容:

> 5 <= "5"
true

Which means that the = part of the <= is treated the same way == is, not ===. Which made me immediately try <== hoping it would do what you would hope it would. But it doesn't exist.

这意味着<=的=部分以相同的方式处理==是,而不是===。这让我立即尝试<==希望它会做你希望它会做的事情。但它不存在。

Then I tried the following:

然后我尝试了以下内容:

> 5 < "6"
true

Then I started to observe even stranger behaviour:

然后我开始观察甚至更奇怪的行为:

> 5 < [6]
true

Which brings up a more important question: are there type-safe equivalents to <, >, <=, and >=?

这提出了一个更重要的问题:<,>,<=和> =是否存在类型安全的等价物?

2 个解决方案

#1


8  

No, but it can be handled by correct use of existing language features to type check.

不,但可以通过正确使用现有语言功能来进行类型检查来处理。

Comparison is ideally two state logic. Either a<b or it is not. The problem is that combining type checking with comparison changes two state logic into three state (true/false/incomparable). To return one of three outcomes would no longer be a simple Boolean.

理想情况下,比较是两种状态逻辑。要么 ,要么不是。问题是将类型检查与比较相结合将两个状态逻辑改变为三个状态(真>

A pre-check on types can already be implemented with typeof or instanceOf

可以使用typeof或instanceOf实现对类型的预检查

If comparisons must be type-appropriate, and there is no code written to deal with mismatches, then an error can be thrown to stop execution as in the following example:

如果比较必须是类型相关的,并且没有编写代码来处理不匹配,则可以抛出错误来停止执行,如下例所示:

if (typeof(a) !== typeof(b)) throw "type mismatch in some_function(), types:"+typeof(a)+','+typeof(b);
// now the next operation is "safe"
if (a<=b) { do_something() } else { do_the_other_thing(); }

Later when there is error handling code, you can replace the throw or keep the throw and use try/catch.

稍后当有错误处理代码时,你可以替换throw或保持throw并使用try / catch。

#2


1  

No, there's nothing built in to do so.

不,没有内置的东西可以做到这一点。

Consider:

// I invented ~ as the non type coercion operator
5 <~ 6
5 <~ '6'

Both of these return false, but the return values don't really mean the same thing. In the second case, it likely wouldn't have even compared the values.

这两个都返回false,但返回值并不意味着同样的事情。在第二种情况下,它甚至可能不会比较这些值。

#1


8  

No, but it can be handled by correct use of existing language features to type check.

不,但可以通过正确使用现有语言功能来进行类型检查来处理。

Comparison is ideally two state logic. Either a<b or it is not. The problem is that combining type checking with comparison changes two state logic into three state (true/false/incomparable). To return one of three outcomes would no longer be a simple Boolean.

理想情况下,比较是两种状态逻辑。要么 ,要么不是。问题是将类型检查与比较相结合将两个状态逻辑改变为三个状态(真>

A pre-check on types can already be implemented with typeof or instanceOf

可以使用typeof或instanceOf实现对类型的预检查

If comparisons must be type-appropriate, and there is no code written to deal with mismatches, then an error can be thrown to stop execution as in the following example:

如果比较必须是类型相关的,并且没有编写代码来处理不匹配,则可以抛出错误来停止执行,如下例所示:

if (typeof(a) !== typeof(b)) throw "type mismatch in some_function(), types:"+typeof(a)+','+typeof(b);
// now the next operation is "safe"
if (a<=b) { do_something() } else { do_the_other_thing(); }

Later when there is error handling code, you can replace the throw or keep the throw and use try/catch.

稍后当有错误处理代码时,你可以替换throw或保持throw并使用try / catch。

#2


1  

No, there's nothing built in to do so.

不,没有内置的东西可以做到这一点。

Consider:

// I invented ~ as the non type coercion operator
5 <~ 6
5 <~ '6'

Both of these return false, but the return values don't really mean the same thing. In the second case, it likely wouldn't have even compared the values.

这两个都返回false,但返回值并不意味着同样的事情。在第二种情况下,它甚至可能不会比较这些值。