Labels

Thursday, July 7, 2011

Associative arrays in ActionScript 3

The Array class provides methods and properties that work with number-indexed arrays only--and not with associative arrays. Associative arrays should be instances of the Object class.

Technically, since the Object class is the base class for all ActionScript classes, all ActionScript objects can be used as associative arrays. However, unless you have some specific reason for using another class as an associative array, it is best to simply use the generic Object class.

var memebers:Object = {scribe: "Franklin",
                      chairperson: "Gina",
                       treasurer: "Sindhu"};
var members:Object = new Object( );
members.scribe = "Franklin";
members.chairperson = "Gina";
members.treasurer = "Sindhu";
var members:Object = new Object( );
members["councilperson"] = "Ruthie";
trace(members.councilperson);         // Displays: Ruthie
members.councilperson = "Rebecca";
trace(members["councilperson"]);      // Displays: Rebecca
More information at the source:
http://digitalmedia.oreilly.com/helpcenter/actionscript30cookbook/chapter5.html?page=8