User Guide

Syntax 73
An array literal can be used to initialize an array. The following examples show two arrays that
are initialized using array literals. You can use the
new statement and pass the compound
literal as a parameter to the Array class constructor, but you can also assign literal values
directly when instantiating instances of the following ActionScript core classes: Object, Array,
String, Number, int, uint, XML, XMLList and Boolean.
// Use new statement.
var myStrings:Array = new Array(["alpha", "beta", "gamma"]);
var myNums:Array = new Array([1,2,3,5,8]);
// Assign literal directly.
var myStrings:Array = ["alpha", "beta", "gamma"];
var myNums:Array = [1,2,3,5,8];
Literals can also be used to initialize a generic object. A generic object is an instance of the
Object class. Object literals are enclosed in curly braces (
{}) and use the comma to separate
object properties. Each property is declared with the colon character (
:), which separates the
name of the property from the value of the property.
You can create a generic object using the
new statement, and pass the object literal as a
parameter to the Object class constructor, or you can assign the object literal directly to the
instance you are declaring. The following example creates a new generic object and initializes
the object with three properties (
propA, propB, and propC), each with values set to 1, 2, and
3, respectively.
// Use new statement.
var myObject:Object = new Object({propA:1, propB:2, propC:3});
// Assign literal directly.
var myObject:Object = {propA:1, propB:2, propC:3};
For more information, see “Creating strings” on page 209, “Introduction to Regular
Expressions” on page 286, and “Initializing XML variables” on page 319.
Semicolons
You can use the semicolon character (;) to terminate a statement. Alternatively, if you omit
the semicolon character, the compiler will assume that each line of code represents a single
statement. Because many programmers are accustomed to using the semicolon to denote the
end of a statement, your code may be easier to read if you consistently use semicolons to
terminate your statements.
Using a semicolon to terminate a statement allows you to place more than one statement on a
single line, but this may make your code more difficult to read.