[英]Can someone explain me why “operator precedence” applies to logical operators like “||”, “&&” in javaScript
Can someone explain me why operator precedence applies to logical operators like ||
有人能解释一下为什么运算符优先级适用于
||
等逻辑运算符吗? and &&
in JavaScript?和
&&
在 JavaScript 中? What does that mean in an operation like:这在以下操作中意味着什么:
true ||真 || false && false
假&&假
the false && false
is evaluated first because the &&
operator is having a higher precedence than the ||
首先评估
false && false
因为&&
运算符的优先级高于||
operator in JavaScript. JavaScript 中的操作员。 according to how I know the
false && false
is not evaluated by the JavaScript engine because before the ||据我所知,JavaScript 引擎没有评估
false && false
因为在 || 之前operator there is a true
literal and when something is true
before the ||
运算符有一个
true
的文字,当||
之前的某些东西为true
时operator the thing after the ||
运算符
||
后面的东西operator will not be evaluated this is called "short-circuiting of logical operators" in JavaScript another example will be:运算符不会被评估这在 JavaScript 中称为“逻辑运算符的短路”,另一个示例是:
true ||真 || alert()
警报()
the function call never takes place even though the function call is having higher precedence than the ||
function 调用永远不会发生,即使 function 调用的优先级高于
||
operator and another example is运算符,另一个例子是
true ||真 || x = 7
x = 7
if short-circuiting of logical operators is true in JavaScript then the above code must not give an error because the x = 7 is not evaluated, since before the ||
如果逻辑运算符的短路在 JavaScript 中为真,那么上面的代码不能给出错误,因为x = 7没有被评估,因为在
||
之前operator there is a true
literal.运算符有一个
true
的文字。
Operator precedence just determines grouping, not actual evaluation order: https://stackoverflow.com/a/46506130运算符优先级仅确定分组,而不是实际的评估顺序: https://stackoverflow.com/a/46506130
true || false && false
true || false && false
becomes true || (false && false)
true || false && false
成true || (false && false)
true || (false && false)
but is still evaluated from left to right. true || (false && false)
但仍从左到右进行评估。
true || alert()
true || alert()
is evaluated as true || (alert())
true || alert()
被评估为true || (alert())
true || (alert())
and NOT (true || alert)()
true || (alert())
和 NOT (true || alert)()
true || x = 7
true || x = 7
is evaluated as (true || x) = 7
and causes an error, NOT true || (x = 7)
true || x = 7
被评估为(true || x) = 7
并导致错误,不true || (x = 7)
true || (x = 7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.