EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 126

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
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");
}
NOTE
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.
TIP
Braces are added to this statement if you click the Check Syntax button.

Table of Contents

Related product manuals