Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Syntax and Language Fundamentals > About language punctuators > About literals | |||
A literal is a value that appears directly in your code. Literals are constant (unchanging) values within your Flash documents. Examples of a literal include true, false, 0, 1, 52, or even the string "foo".
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 punctuators ([]) and use the comma punctuator (,) 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 built-in ActionScript class.
// using new statement
var myStrings:Array = new Array("alpha", "beta", "gamma");
var myNums:Array = new Array(1, 2, 3, 5, 8);
// assigning 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 punctuator (,) to separate object properties. Each property is declared with the colon punctuator (:), 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});
// assigning literal directly
var myObject:Object = {propA:1, propB:2, propC:3};
Do not confuse a string literal with a String object. In the following example, the first line of code creates the string literal firstStr, and the second line of code creates the String object secondStr:
var firstStr:String = "foo"
var secondStr:String = new String("foo")
Use string literals unless you specifically need to use a String object for better performance. For more information on strings, see About strings and the String class.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000689.html