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
statement and pass the object literal as a
new
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,
,
, and
, each with values set to
,
, and
,
propA
propB
propC
1
2
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
, and the second line of code creates the String object
firstStr
:
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" on page
450.
About comments
Comments are a way of annotating your code with plain-English descriptions that do not get
evaluated by the compiler. You can use comments within your code to describe what the code
is doing or to describe which data returns to the document. Using comments can help you
remember important coding decisions, and it can be helpful to anyone else who reads your
code. Comments must clearly explain the intent of the code and not just translate the code. If
something is not readily obvious in the code, you should add comments to it.
Using comments to add notes to scripts is highly recommended. Comments document the
decisions you make in the code, answering both how and why. They make ActionScript easier
to understand. For example, you might describe a work-around in comments. Therefore, you
or another developer can easily find sections of code to update or fix. Or, if the issue is fixed or
improved in a future version of Flash or Flash Player, you could improve the ActionScript by
removing the work-around.
About language punctuators
131
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?