1.21 gigawatts
1.21 gigawatts

Reputation: 17878

Why is my for loop not working on object?

My for loop is not going through any properties of my object. Is there something I'm missing here?

public var previousPresets:HTMLExportOptions;

public function savePresets():void {
        if (previousPresets==null) {
            previousPresets = new HTMLExportOptions();
        }

        // set properties
        for (var property:String in previousPresets) {
            if (property in this) {
                previousPresets[property] = this[property];
            }
        }
    }

And the class:

public class HTMLExportOptions extends ExportOptions {

    public function HTMLExportOptions() {

    }

    public var addZoom:Boolean;
    public var showScreenshotBackground:Boolean;
    public var showFullHTMLPageSource:Boolean;
    public var useInlineStyles:Boolean;
    public var showBorders:Boolean;
    public var showBordersCSS:String;
}

More Related Questions:

Do you always have to set property enumerable for it to work? Would making my class explicitly extend Object cause any changes? How about marking it dynamic?

Upvotes: 0

Views: 144

Answers (2)

1.21 gigawatts
1.21 gigawatts

Reputation: 17878

Using describeType sort of works but it still needed some work and organization. This method gets an XML List of the properties and where they were declared.

var properties:Array = getPropertiesArray(options);


public static function getPropertiesArray(object:Object, sort:Boolean = true):Array {
    var describedTypeRecord:DescribeTypeCacheRecord = mx.utils.DescribeTypeCache.describeType(object);
    var typeDescription:* = describedTypeRecord.typeDescription;
    var hasFactory:Boolean = typeDescription.factory.length()>0;
    var factory:XMLList = typeDescription.factory;
    var itemsLength:int;
    var itemsList:XMLList;
    var propertyName:String;
    var properties:Array = [];

    itemsList = hasFactory ? factory.variable + factory.accessor : typeDescription.variable + typeDescription.accessor;
    itemsLength = itemsList.length();

    for (var i:int;i<itemsLength;i++) {
        var item:XML = XML(itemsList[i]);
        propertyName = item.@name;
        properties.push(propertyName);
    }

    if (sort) properties.sort();

    return properties;
}

Upvotes: 0

null
null

Reputation: 5265

The documentation of the for..in statement states the following:

Iterates over the dynamic properties of an object or elements in an array and executes statement for each property or element. Object properties are not kept in any particular order, so properties may appear in a seemingly random order. Fixed properties, such as variables and methods defined in a class, are not enumerated by the for..in statement. To get a list of fixed properties, use the describeType() function, which is in the flash.utils package.

The real question is why you have the properties on both this and previousPresets.

Instead of creating the previousPresets object only when calling that method savePresets(), have it from the beginning and always save the values into that object instead of this. Why bloat this with all those values?

Upvotes: 1

Related Questions