For a long time I’ve struggled with determining whether or not a property exists in Actionscript 3.0. Like many people, I would often find myself writing somewhat tediously repetitive conditional checks such as:
1 2 3 4 5 |
if (myObject["myProperty"] !=null && myObject["myProperty"] != undefined) { trace("Do the happy dance, my property has a value!"); } |
Other than the fact that this is a lot of code to write (especially when you have a long conditional that checks 10 or 15 properties), this would work “most” of the time. I say most, because if the property you were trying to validate didn’t exist at all (i.e. there is no property name “myProperty” in your object), then you would get a nice “null object reference” error.
Continue Reading