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

前端 未结 3 1434
盖世英雄少女心
盖世英雄少女心 2021-02-18 23:01

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 su

相关标签:
3条回答
  • 2021-02-18 23:43

    Use something along the lines of

    if (myObject.hasOwnProperty("propertyName"))
    

    to check if the property exists.

    Edit: Also take a look here.

    0 讨论(0)
  • 2021-02-18 23:45

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

    You should use

    if ("propertyName" in myObject)
    

    instead.

    0 讨论(0)
  • 2021-02-18 23:54

    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().

    0 讨论(0)
提交回复
热议问题