actionscript (flex): how to know whether a property of object exists (or defined)?

为君一笑 提交于 2020-01-01 07:36:19

问题


I am a Java developer who tries Flex. Here is my problem:

I behave actionScript objects as hashmap but when the object do not have the property it gives exception: No such variable.

Here I expect it gave me null, instead of giving exception. So do you know is there a way to handle it, namely check if the property is defined for object.

trace( obj["2008-02"] ) // gives exception


回答1:


Use something along the lines of

if (myObject.hasOwnProperty("propertyName"))

to check if the property exists.

Edit: Also take a look here.




回答2:


hasOwnProperty() doesn't work correctly with inheritance, static properties, or dictionaries.

You should use

if ("propertyName" in myObject)

instead.




回答3:


try

if ( obj["2008-02"] != null ) { then do something }

it is null, but you can't output null. you can also try converting it to a string for the purposes of a trace().



来源:https://stackoverflow.com/questions/1520610/actionscript-flex-how-to-know-whether-a-property-of-object-exists-or-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!