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
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 (
object properties. Each property is declared with the colon character (
name of the property from the value of the property.
You can create a generic object using the
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 (
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
Expressions" on page
Semicolons
You can use the semicolon character (
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.
new
,
propA
propB
"Creating strings" on page
286, and
"Initializing XML variables" on page
) to terminate a statement. Alternatively, if you omit
;
statement and pass the compound
new
) and use the comma to separate
{}
statement, and pass the object literal as a
, and
), each with values set to 1, 2, and
propC
209,
"Introduction to Regular
), which separates the
:
319.
Syntax
73