简体   繁体   English

有人能解释一下为什么“运算符优先级”适用于 javaScript 中的“||”、“&&”等逻辑运算符吗

[英]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的文字,当||之前的某些东西为trueoperator 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 && falsetrue || (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.

相关问题 有人可以向我解释逻辑“OR”运算符的范围在 Javascript 中是如何工作的吗? - Can someone explain to me how the scope of the logical "OR" operator works in Javascript? 有人可以向我解释适用于此的 JavaScript 中的规则吗? - Can someone explain to me the rule in JavaScript that applies here? JavaScript 中的运算符优先级:有人可以解释为什么 if 条件对于浏览器的所有值都为 true - operator precedence in JavaScript : Can someone please explain why the if condition evaluates to true for all values of browser JavaScript中具有三元条件和逻辑的运算符优先级和运算符 - Operator Precedence with Ternary Conditional and Logical And operators in JavaScript 有人可以解释一下 ECMAScript 规范中提到运算符优先级和运算符关联性的地方吗 - can someone explain me where in the ECMAScript specification operator precedence and operator associativity is mentioned 有人可以用javascript解释逻辑运算符吗 - Can some one explain logical operators in javascript 有人可以向我解释为什么JavaScript的这种局限性行不通吗? - Can someone explain to me why this inheiritance in javascript doesnt work? 有人能解释一下像“for (;;)”这样的循环的语法吗 - Can someone explain me the Syntax of a loop like “for (;;)” 有人可以向我解释此Javascript Singleton代码吗? - Can someone explain this Javascript Singleton code to me? 有人可以向我解释这种JavaScript行为吗? isNaN([]) - Can someone explain me this javascript behaviour? isNaN([])
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM