Order of Operations Strikes Again
By Roger Braunstein | on March 27, 2007
In AS3, Flash, Flex, Programming, Tutorial |
Quick AS3 hint for today. Keep learning from my mistakes! Did you know that the order of the ! operator is higher than the order of the is operator? That means that in the example:
if (!strategy is AwesomeStrategy) {...
in which I am trying to run some code if the strategy is not an instance of AwesomeStrategy, actually evaluates:
if (null is AwesomeStrategy) {...
if (false) {...
In other words, the not binds higher and applies to strategy instead of strategy is AwesomeStrategy, the condition evaluates to false 100% of the time, and you end up puzzled as to why the block in the if statement didn’t run. Don’t make my mistake! Include parentheses when negating comparisons! Use this instead:
if (!(strategy is AwesomeStrategy)) {...
That’s right, you heard it here first. Unless you aren’t as tired as me right now and already knew that. Actually, as I look at my incorrect code now, it seems pretty obviously incorrect. OH WELL!
See the whole order of operations in AS3 here.
No Comments yet »
RSS feed for comments on this post. TrackBack URI