views:

45

answers:

1

In actionscript object class can act as a collection that stores key,value combinations:

var o:Object = new Object();
o["a"] = 1;

But when I'm trying to extend it and add some custom functionality:

var mo:MyObject = new MyObject();
mo["a"] = 1;

I get this:

ReferenceError: Error #1056: Cannot create property a on MyObject.

How would I solve this? Thanks.

+2  A: 

You need to make the MyObject class a dynamic class.

package foo.bar {
    public dynamic class MyObject {
    }
}

A dynamic class supports the Object behavior of <String,Object> -- to get the arbitrary <Object,Object> map, you need to extend Dictionary instead (again, making the class dynamic).

Michael Brewer-Davis
Great, thanks. A bit unrelated question, but is there a way to get a count of properties (keys) without iterating through the whole object?
negative
http://stackoverflow.com/questions/284947/how-do-i-find-the-length-of-an-associative-array-in-actionscript-3-0
Michael Brewer-Davis