Section 1.8. define arrays and objects

Source: Internet
Author: User
Tags script tag

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.