不含咖啡因:双括号 [英] Decaffeinated: Double Brackets

查看:270
本文介绍了不含咖啡因:双括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近对一个旧项目进行了脱除咖啡因的处理,我发现我有很多 if子句,其中的表达式用额外括号包裹:

I decaffeinated an old project recently and I noticed that I got a lot of if clauses where the expressions are wrapped in "extra" brackets:

if ((data == null) || (data === ""))

在任何情况下都需要包装吗?恕我直言,它与:

Is there any case where the wrapping is required? Imho it is the same as:

if (data == null || data === "")


推荐答案

在这种情况下没关系,但是只要您从 if 语句(或几乎任何地方),请确保您选中优先级表

In that case it wouldn't matter, but whenever you remove parentheses from an if statement (or anywhere pretty much) make sure you check the precedence table.

例如,删除括号:

if ((someVar && someConditional) == someBool)

将导致:

if (someVar && someConditional == someBool)

这是完全不同的。由于括号,第一个示例将评估 someVar&&首先是someConditional ,然后是 resultOfOperation == someBool 。在第二个示例中,由于&& 的优先级较高,因此首先评估 someConditional == someBool ,然后 0&& resultOfOperation

Which is completely different. The first example, due to the parentheses, will evaluate someVar && someConditional first, then resultOfOperation == someBool. In the second example, due to the higher precedence of &&, someConditional == someBool is evaluated first, then 0 && resultOfOperation.

这篇关于不含咖啡因:双括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆