Wednesday, January 16, 2013

Literals

A literal is a value that appears directly in your code. The following examples are all literals:
17
"hello"
-3
9.4
null
undefined
true
false
Literals can also be grouped to form compound literals. Array literals are enclosed in bracket characters ([]) and use the comma character (,) to separate array elements. 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 any ActionScript core class.
// 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 character (,) 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.
// using 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};
Programming Actionscript 3. Powered by Blogger.