2011-04-01 41 views
6

来到这个JS片段,我真的不知道什么顺序的事情正在评估... 任何想法?括号将是有益的......Javascript订单评估的

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

FWIW:https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence – Shog9 2011-04-01 13:47:32

+0

现在,它的格式,它更有感!它最初看起来像这样,没有换行符:return point [0]> = -width/2 - allowance && point [0] <= width/2 + allowance && point [1]> = --height/2 - allowance && point [ 1] <=身高/ 2 +津贴; – whatsgoingon 2011-04-01 13:49:25

+1

如果你已经破译了评估顺序,如果你将这个代码的功能联系起来,它可能会帮助未来的读者。 – elbillaf 2011-04-01 13:57:30

回答

2

等同于:

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

这个怎么样:'-width/2 - allowance'? '/'运算符优先于第二个'-' – 2011-04-01 13:49:57

+0

美丽!谢谢! – whatsgoingon 2011-04-01 13:50:14

+0

这将触发Javascript分号插入错误,除非您在与return语句相同的行上开始表达式。 – hugomg 2011-12-06 19:42:14

0

添加paratheses有的缩进应该更清楚:

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

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