简体   繁体   English

ActionScript 3.0(Flex)中条件的评估顺序

[英]Order of evaluation of conditionals in actionscript 3.0 (Flex)

var btn:Button;
if(btn != null  &&  btn.label != '') {
      mx.controls.Alert.show("d");  
}

In the above if clause, is it guaranteed that the first condition(btn != null) will be evaluated before the second condition? 在上面的if子句中,是否可以保证在第二个条件之前先评估第一个条件(btn!= null)?

Yes - ActionScript performs appropriate short-circuiting for the && operator: 是-ActionScript为&&运算符执行适当的短路:

So not only will it evaluate the expressions in the order you described, it won't bother evaluating the second expression at all if the first one returns false (which is as important a detail as the order of evaluation). 因此,它不仅会按照您描述的顺序对表达式进行求值,而且如果第一个表达式返回false (它与求值顺序同样重要),它也不会打扰第二个表达式。

Just as a note, ActionScript also supports short-circuiting the logical or operator ( || ) . 值得注意的是,ActionScript还支持将逻辑或运算符( || )短路

Yes it is. 是的。 Excerpts from the Adobe livedocs regarding && operator: Adobe livedocs中&&运算符有关的节选:

&& logical AND Operator : && 逻辑AND运算符

Usage: 用法:

expression1 && expression2

Returns expression1 if it is false or can be converted to false , and expression2 otherwise. 返回expression1如果是false或可转化为false ,和expression2否则。

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

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