126
Syntax and Language Fundamentals
You can declare the data type of objects based on built-in classes (Button, Date, MovieClip,
and so on) and on classes and interfaces that you create. In the following snippet, you create a
new object of the custom type Student:
var firstStudent:Student = new Student();
You can also specify that objects are of the Function or the Void data type. For more
information on assigning data types, see
Chapter 4, “Data and Data Types,” on page 71
.
Curly braces
You group ActionScript events, class definitions, and functions into blocks using curly brace
(
{}
) punctuators. You put the opening brace on the same line as the declaration.
Place braces around each statement when it is part of a control structure (such as
if..else
or
for
), even if it contains only a single statement. This good practice helps you avoid errors in
your ActionScript when you forget to add braces to your code. The following example shows
code that is written using poor form:
var numUsers:Number;
if (numUsers == 0)
trace("no users found.");
Although this code validates, it is considered poor form because it lacks braces around
the statements.
In this case, if you add a second statement after the trace statement, the second statement
executes regardless of whether the
numUsers
variable equals 0, which can lead to unexpected
results. For this reason, add braces so the code looks like the following example:
var numUsers:Number;
if (numUsers == 0) {
trace("no users found");
}
NO
TE
You can also put the opening brace on the line that follows the declaration. Coding
conventions recommend that you put the opening brace on the same line for
consistency. For information on braces and code conventions, see
Chapter 19, “Best
Practices and Coding Conventions for ActionScript 2.0,” on page 731
.
TI
P
Braces are added to this statement if you click the Check Syntax button.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...