我可以使用链式比较运算符语法吗?

发布于 2024-11-28 12:24:53 字数 291 浏览 0 评论 0原文

在一个 JS 库中,我看到了这样的语法:

if (val > 5 == t) { ... }

我在控制台中测试了这个:

1 == 1 == 2 // false
2 > 1 == 1  // true
1 == 2 == 1 // false
1 == 1 == 1 // true
1 < 2 < 3   // true
1 > 2 > 3   // false

乍一看一切正确。这个可以用吗?

In one JS library I saw such syntax:

if (val > 5 == t) { ... }

I tested this in console:

1 == 1 == 2 // false
2 > 1 == 1  // true
1 == 2 == 1 // false
1 == 1 == 1 // true
1 < 2 < 3   // true
1 > 2 > 3   // false

At first glance all correct. Can this be used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨小沫ゞ 2024-12-05 12:24:53
1 == 1 == 2  // this
true == 2    // becomes this
1 == 2       // which becomes this, and is false
2 > 1 == 1  // this
true == 1   // becomes this
1 == 1      // which becomes this, and is true

...等等。

如果您想了解转换,您应该搜索 == 运算符,该运算符使用 抽象相等比较算法

1 == 1 == 2  // this
true == 2    // becomes this
1 == 2       // which becomes this, and is false
2 > 1 == 1  // this
true == 1   // becomes this
1 == 1      // which becomes this, and is true

...and so on.

If you're wondering about the conversion, you should do a search on the == operator, which uses the Abstract Equality Comparison Algorithm.

濫情▎り 2024-12-05 12:24:53

JavaScript 不支持数学中使用的链式比较语法:

1 < 2 < 3 // 1 is less than 2 which is less than 3.

相反,它从左到右评估每个比较,这有时会产生与数学链式比较相同的结果,就像所有示例一样,但过程不同:

1 < 2 < 3 // "1 is less than 2" is true, true is 1, "1 is less than 3" is true.
          // Javascript returns true.

3 < 2 < 1 // "3 is less than 2" is false, false is 0, "0 is less than 1" is true.
          // Javascript returns true.

出于这个原因,应该劝阻。

不过,要回答你的问题,是的,可以使用它。

Javascript does not support the chained comparison syntax used in mathematics:

1 < 2 < 3 // 1 is less than 2 which is less than 3.

Instead, it evaluates each comparison left to right, which can sometimes yield the same results as mathematical chained comparison, as do all your examples, but the process is different:

1 < 2 < 3 // "1 is less than 2" is true, true is 1, "1 is less than 3" is true.
          // Javascript returns true.

3 < 2 < 1 // "3 is less than 2" is false, false is 0, "0 is less than 1" is true.
          // Javascript returns true.

For this reason, it should be discouraged.

To answer your question, however, yes it can be used.

揽月 2024-12-05 12:24:53

您并没有真正比较您认为正在比较的内容:

(1 == 1) == 2 // actually true == 2 which is false

(1 == 2) == 1 // actually false == 1 which is false

这就是为什么严格相等 === 在所有情况下都会失败

You not really comparing what you think you are comparing:

(1 == 1) == 2 // actually true == 2 which is false

(1 == 2) == 1 // actually false == 1 which is false

Which is why strict equality === will fail in all cases

我们的影子 2024-12-05 12:24:53

这是一种正确的语法,但不是我推荐的语法。发生的情况可能是:

if ((val > 5) == t) { ... }

I tested this in console:

(1 == 1) == 2 // false
(2 > 1) == 1  // true
(1 == 2) == 1 // false
(1 == 1) == 1 // true
(1 < 2) < 3   // true
(1 > 2) > 3   // false

左侧的布尔值隐式转换为 int。

It's a correct syntax but not one I would recommend. What's happening, is probably:

if ((val > 5) == t) { ... }

I tested this in console:

(1 == 1) == 2 // false
(2 > 1) == 1  // true
(1 == 2) == 1 // false
(1 == 1) == 1 // true
(1 < 2) < 3   // true
(1 > 2) > 3   // false

With the boolean on the left implicitly converted to an int.

昔日梦未散 2024-12-05 12:24:53

没有什么可以阻止您使用这种冒险的语法,但是请记住,JavaScript 代码中出现错误的最常见原因之一就是搞乱 运算符优先级

因此,强烈建议通过向优先级组添加括号来显式定义优先级,以防它们包含多个优先级可明确确定的简单数学表达式。

There is nothing to stop you from using such an adventurous syntax however do keep in mind that one of the most frequent reasons for having bugs inside JavaScript code is messing up operator precedence.

Therefore its strongly advised to explicitly define precedence by adding brackets to precedence groups in case they consist of more than simple mathematical expressions for which the precedence is clearly determinable.

浅紫色的梦幻 2024-12-05 12:24:53

另一个问题是类型强制。
jslint 输出:

错误:
第 2 行第 13 行字符出现问题:预期为 '===',结果却看到
'=='。

if (val > 5 == t) { }

Another concern is type coercion.
jslint output:

Error:
Problem at line 2 character 13: Expected '===' and instead saw
'=='.

if (val > 5 == t) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文