4 Replies Latest reply on Jun 22, 2009 11:00 PM by leybniz

    How to access all properties of a generic object

    DaveFastFingers Level 1

      I am trying to create a simple tester utility to allow editing of UI attributes at runtime and I want to get a list of all of an objects attributes and display them in a Tree.

       

      Many examples of this are found with a google search, but I can't understand why the 1st example below does not work (no properties are identified by the for loop), and the second example works as expected (the  object Tree was chosen arbitrarily, and cleary has many attributes).

       

      Example 1: Typed objects don't seem to work, no output from this code.

       

                      var testTree:Tree = new Tree();
                      for( var o : * in testTree){
                          trace( o + " = " + testTree[o] );
                      }

       

       

      Example 2: This works as expected

                    
                      var dynamicObj:Object = { param1:"test1", param2:"test2" };
                      for( var x : * in dynamicObj){
                          trace( x + " = " + dynamicObj[x] );
                      }  

        • 1. Re: How to access all properties of a generic object
          leybniz Level 4

          Example 1, is not working because by default SDK classes marked them as non enumerable.

          Object.setPropertyIsEnumerable('',false);

           

          there are few other alternatives on how to introspect objects:

          describeType();

          DescribeTypeCache

          ObjectUtil.getClassInfo();

          1 person found this helpful
          • 2. Re: How to access all properties of a generic object
            CoreyRLucier Adobe Employee

            In this case Tree is a sealed class.  In AS3 for..in only enumerates dynamic properties.

             

            As Alexander points out there are several APIs you can use.

             

            The FLEX SDK provides the mx.utils.ObjectUtil class which is a wrapper around describeType (among other things).  It also utilizes a cache so that multiple calls to getClassInfo are fast.

             

            To enumerate properties of an arbitrary object:

             

                    var classInfo:Object = ObjectUtil.getClassInfo(value);

                    for each (var p:* in classInfo.properties)
                    {

                           ...

                    }

            1 person found this helpful
            • 3. Re: How to access all properties of a generic object
              DaveFastFingers Level 1

              Hey Guys, thanks for the help, your posts have been immensely helpful and now that I have a basic understanding I've been able to find plenty of relevant examples, blogs, etc. via google.

               

              One quick followup question though. I did find this in a nice example that included the following code:

               

                          public function getMyProperties(event:MouseEvent):void {

                              myProperties = describeType(imgPhoto)..accessor;            }

               

              I come from a Java background and haven't seen the ".." syntax used before. Can someone explain what I'm seeing there. I'm self-learning flex/actionscript and couldn't find a reference to this on google (it's not obvious what keywords I should search on).

               

              Thanks!

              David

              • 4. Re: How to access all properties of a generic object
                leybniz Level 4

                ".." means XMLList of all nodes having name accessor

                the same effect could be achieved using this syntax describeType(imgPhoto).descendants("accessor");

                find out more at XML type documentation:

                http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_01.html

                 

                If you feel this message answers your question or helps, please mark it respectively