Javascript评估顺序

时间:2011-04-01 13:44:58

标签: javascript operator-precedence

遇到这个JS片段,老实说,我不知道正在评估的内容是什么...... 有任何想法吗?括号会很有用......

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

5 个答案:

答案 0 :(得分:2)

相当于:

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

答案 1 :(得分:2)

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

相关运算符按此顺序排列:一元否定,除法,加法/减法,关系(&gt; =,&lt; =),逻辑和。

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

答案 2 :(得分:2)

检查这个

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);

答案 3 :(得分:0)

添加括号和一些缩进应该更清楚:

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

答案 4 :(得分:0)

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