Why does short-circuit evaluation work when operator precedence says it shouldn't?

前端 未结 3 877
感动是毒
感动是毒 2021-01-12 05:25

In JavaScript and Java, the equals operator (== or ===) has a higher precedence than the OR operator (||). Yet both languages (JS, Jav

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 06:18

    There is no operator precedence in this case. What you are questioning is like in f(callback) statement the callback function being evaluated even before f. This can not happen.

    On the other hand, in JS the || is one of the few places where you can watch laziness at show. The == operand (think as if it is an infix function like in fully functional languages) takes two arguments and the one on the left gets evaluated first. If it resolves to true the second argument doesn't even get evaluated.

提交回复
热议问题