How to make an user defined Object Iterable in AS3

2019-08-02 00:47发布

问题:

I need to iterate through all the properties of an user defined object. How to make an Object Iterable in As3? (I want to use that in a for ... each loop).

I tried to extend Proxy.

But not sure what methods to implement for this.

below is my class,

[Bindable]
public class XYZ extends Proxy
{
    public var aggTerRedQty:String;

    public var aggNBPQtyUsrs:String;

    public var termAllocUnloadQty:String;

    public var effNomQty:String;

    public var termAllocSplitFirm:String;

}

Thanks

回答1:

[Edit]

I made a mistake in the code to support a for each loop, editing to fix that.

Based on the documentation for the Proxy class, I whipped up this example. I've never done it before, and the implementation seems a bit odd. But this code is mostly from the documentation, and it works: you can use a for each loop or a for in loop on the class that extends Proxy.

Main class:

package 
{ 
    import flash.display.Sprite; 

    public class Main extends Sprite  
    { 
        public function Main():void 
        { 
            var obj:MyClass = new MyClass();

            trace("for in loop output:");
            for (var propertyName:String in obj)
            {
                trace("property: " + propertyName + "  value: " + obj[propertyName]);
            }

            trace("for each loop output:")
            for each (var item:Object in obj)
            {
                trace("current item: " + item);
            }
        } 
    } 
}

Class that extends Proxy:

package
{
    import flash.display.Sprite;
    import flash.utils.Proxy;
    import flash.utils.flash_proxy;

    use namespace flash_proxy;

    public class MyClass extends Proxy
    {
        protected var _target:Object = { property1: property1, property2: property2, property3: property3, property4: property4 };
        protected var _item:Array;

        public function MyClass()
        {
        }

        override flash_proxy function nextNameIndex (index:int):int { 
            // initial call
            if (index == 0)
            {
                _item = new Array();
                for (var x:* in _target)
                {
                    _item.push(x);
                }
            }
            if (index < _item.length)
            {
                return index + 1;
            }
            else
            {
                return 0;
            }
        }

        override flash_proxy function nextName(index:int):String
        {
            return _item[index - 1];
        }

        override flash_proxy function nextValue(index:int):*
        {
            return _target[ _item[index -1] ];
        }

        override flash_proxy function getProperty(name:*):*
        {
            return _target[name];
        }


        public var property1:String = "Hi";
        public var property2:Boolean = true;
        public var property3:Object = { a: 1, b: false };
        public var property4:Sprite;
    }
}

Console Output:

for in loop output:
property: property2  value: true
property: property4  value: null
property: property1  value: Hi
property: property3  value: [object Object]
for each loop output:
current item: true
current item: null
current item: Hi
current item: null

On further review of the documentation, a realistic implementation of the Proxy class would probably also need to implement the Proxy class' setProxy() method as well.



回答2:

Easiest way - create Array or Vector., or Dictionary if you want access by name, populate it with the properties, create a function in that class that will iterate through the array/Vector/Dictionary.

Example:

public class Example
{
  public var _properties:Vector.<String>; //this can be Dictionary or Array, depends what you need it for

 public function Example()
 {
   _properties = new Vector.<String>;
   _propertis.push(property1, property2, ...);
 }

 public function findProperty(someCondition:String):String
 {
   for(var i:int = 0; i < _properties.length;i++)
   {
     if(_propertis[i] == someCondition) return _properties[i];
   }
 }
}