为什么在操作员优先表示不应该的情况下进行短路评估? [英] Why does short-circuit evaluation work when operator precedence says it shouldn't?

查看:81
本文介绍了为什么在操作员优先表示不应该的情况下进行短路评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript Java ,等于运算符(=====)具有比OR运算符(||)更高的优先级.然而,两种语言( JS Java )支持if中的短路声明:

In JavaScript and Java, the equals operator (== or ===) has a higher precedence than the OR operator (||). Yet both languages (JS, Java) support short-circuiting in if statements:

当我们拥有if(true || anything())时,不会评估anything().

When we have if(true || anything()), anything() isn't evaluated.

您还可以具有以下表达式:true || foo == getValue())-例如在诸如console.log(...);之类的输出语句中或在赋值中.

You can also have the following expression: true || foo == getValue()) - for example in an output statement such as console.log(...);, or in an assignment.

现在,根据操作员优先级,不应发生短路,因为就优先级而言,=== = ==> ||. (换句话说,应该首先进行比较,应对此调用getValue(),因为相等性检查的优先级高于OR比较.)但是确实如此. getValue()不会被调用(可以通过将输出语句放入其主体来轻松地进行检查).

Now, according to operator precedence, short-circuiting shouldn't happen, as === = == > || in terms of precedence. (In other words, the comparison should happen first, for which getValue() ought to be called, as the equality check has a higher precedence that the OR comparison.) But it does. getValue() isn't called (as can easily be checked by putting an output statement into its body).

为什么(当操作员的优先级说不应这样做时,短路会起作用)吗?
还是让我感到困惑?

Why (does short circuiting work when the operator precedence says it shouldn't)?
Or am I confusing matters?

推荐答案

还是让我感到困惑?

Or am I confusing matters?

你是.我认为将 grouping 作为优先级考虑比排序要容易得多.它会影响评估的顺序,但仅因为会更改分组.

You are. I think it's much simpler to think about precedence as grouping than ordering. It affects the order of evaluation, but only because it changes the grouping.

我不确定是否了解Javascript,但是在Java中,操作数始终按从左到右的顺序求值. ==的优先级高于||的事实意味着

I don't know about Javascript for sure, but in Java operands are always evaluated in left-to-right order. The fact that == has higher precedence than || just means that

true || foo == getValue()

被评估为

true || (foo == getValue())

而不是

(true || foo) == getValue()

如果您只是以这种方式考虑优先级,然后认为求值总是从左到右(例如,||的左操作数总是在右操作数之前求值),那么一切都很简单-和getValue()不会因短路而被评估.

If you just think about precedence in that way, and then consider that evaluation is always left-to-right (so the left operand of || is always evaluated before the right operand, for example) then everything's simple - and getValue() is never evaluated due to short-circuiting.

要消除等式中的短路,请考虑以下示例:

To remove short-circuiting from the equation, consider this example instead:

A + B * C

...,其中ABC可以只是变量,也可以是其他表达式,例如方法调用.在Java中,保证将其评估为:

... where A, B and C could just be variables, or they could be other expressions such as method calls. In Java, this is guaranteed to be evaluated as:

  • 评估A(并记住以备后用)
  • 评估B
  • 评估C
  • 乘以BC
  • 的结果
  • 将计算结果A与乘法结果相加
  • Evaluate A (and remember it for later)
  • Evaluate B
  • Evaluate C
  • Multiply the results of evaluating B and C
  • Add the result of evaluating A with the result of the multiplication

请注意,尽管*的优先级高于+,但仍然会在BC之前评估A.如果您想按顺序考虑优先顺序,请注意乘法在加法之前仍然是如何发生的-但它仍然满足从左到右的求值顺序.

Note how even though * has higher precedence than +, A is still evaluated before either B or C. If you want to think of precedence in terms of ordering, note how the multiplication still happens before the addition - but it still fulfills the left-to-right evaluation order too.

这篇关于为什么在操作员优先表示不应该的情况下进行短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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