Section 1.8. define arrays and objects
1.8.1. Problem
You need to define an array object or hash table-style object to store values or other objects.
1.8.2. Solution
Create a new object or array by calling the constructor using the ActionScript syntax, or define them in mxml
1.8.3. Discussion
Arrays and objects are the two most common types of data that can be stored. They can be defined through ActionScript or in mxml. To define data in mxml, use the <mx: array> label to wrap all data items in the array:
<Mx: array>
<Mx: String> flex </MX: String>
<Mx: String> flash </MX: String>
<Mx: String> Flash Media Server </MX: String>
<Mx: String> Flash Lite </MX: String>
<Mx: String> air </MX: String>
</MX: array>
All data in the array is accessed through indexes. You can also create multidimensional arrays in mxml, such:
<Mx: array>
<Mx: array>
<Mx: String> flex </MX: String>
<Mx: String> flash </MX: String>
<Mx: array>
</MX: array>
To create an object in mxml, you can use the <mx: Object> label and add all object attributes and their values. For example:
<Mx: Object ID = "person" firstname = "John" lastname = "Smith" age = "50" Socialsecurity = "123-45-6789"/>
There is a restriction on creating objects in mxml, that is, you cannot create multiple nested objects. In the script tag, you can create objects that contain multiple complex objects, you can create a variable of the object type before calling the constructor. It is best to add attributes:
VaR object: Object = new object ();
VaR otherobject: Object = new object ();
Object. Other = otherobject;
You can also use braces to create objects, for example:
Code view:
VaR person: Object = {name: "John Smith", age: 22, position: {Department: "accounting ",
Salary: 50000, title: "junior accountant"}, ID: 303 };
Note that the position attribute of the person object points to another object containing different attributes. It is also found that the position object does not need to be stated first.
To create an array in ActionScript, first create a variable and then call the array constructor:
Code view:
VaR arr: array = new array ("red", "blue", "White", "black", "green", "yellow ");
You can also use brackets instead of calling constructors to create data, for example:
VaR noconstructorarray: array = [2, 4, 6, 8, 10, 12, 14, 16];
This is the same as calling the array constructor.