简体   繁体   English

Javascript评估顺序

[英]Javascript order of evaluation

Came across this JS snippet, and I honestly have NO idea of what order things are being evaluated in... Any ideas? 遇到这个JS片段,我真的不知道正在评估什么样的订单......有什么想法吗? Parentheses would be helpful... 括号会有帮助...

return point[0] >= -width / 2 - allowance &&
       point[0] <= width / 2 + allowance && 
       point[1] >= -height / 2 - allowance && 
       point[1] <= height / 2 + allowance;

Equivalent to: 相当于:

return
    (point[0] >= ((-width  / 2) - allowance))
 && (point[0] <= (( width  / 2) + allowance))
 && (point[1] >= ((-height / 2) - allowance))
 && (point[1] <= (( height / 2) + allowance));

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence

The relavant operators go in this order: unary negation, division, addition/subtraction, relational (>=, <=), logical and. 相关运算符按此顺序排列:一元否定,除法,加法/减法,关系(> =,<=),逻辑和。

return (point[0] >= ((-width / 2) - allowance))
    && (point[0] <= ((width / 2) + allowance))
    && (point[1] >= ((-height / 2) - allowance))
    && (point[1] <= ((height / 2) + allowance))

check this 检查一下

function bob(n){
  alert(n);
  return n;
}

return bob(1) >= bob(2) / bob(3) - bob(4) &&
       bob(5) <= bob(6) / bob97) + bob(8) && 
       bob(9) >= bob(10) / bob(11) - bob(12) && 
       bob(13) <= bob(14) / bob(15) + bob(16);

Adding paratheses and some indentation should make it clearer: 添加paratheses和一些缩进应该更清楚:

return 
  point[0] >= (-width / 2) - allowance 
    && 
  point[0] <= (width / 2) + allowance 
    && 
  point[1] >= (-height / 2) - allowance 
    && 
  point[1] <= (height / 2) + allowance;

return (point[0]) >= (-width / 2 - allowance) && (point[0] <= width / 2 + allowance) && (point[1] >= -height / 2 - allowance) && (point[1]) <= (height / 2 + allowance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM