Understanding ActionScript Syntax

Whether you’re writing ActionScript code inline, in an MXML script, or in a class, you’ll need to understand its basic syntax. The following sections look at the basic elements of ActionScript, such as class syntax, variables, statements, expressions, functions, and objects.

Understanding Packages

The majority of classes are organized into structures called packages. To understand most of ActionScript, you must understand what packages are and how you can work with them.

A package groups together classes so that you can ensure uniqueness of scope. For example, you can have only one Button class within a scope. If you tried to declare two Button classes in the same scope, there would be a conflict; the compiler wouldn’t know which one to use.

A package allows you to create several classes with the same name by placing them in different scopes. For example, the Button class that’s part of the Flex framework (i.e., the button UI component) exists within a package called mx.controls. When a class is placed within a package, it has what’s called a fully qualified class name. Therefore, the fully qualified class name for Button is mx.controls.Button. That ensures that if you want to create another Button class in a different package, you can do so without conflicting with mx.controls.Button. For example, mx.controls.Button and com.example.ui.Button (a fictitious class) could exist within the same application without causing a problem.

When classes are in packages, it can be quite cumbersome to have to refer to the class by its fully qualified name. For example, if you want to declare a Button variable, you have to use the following code if you wish to use the fully qualified class name:

var button:mx.controls.Button;

And if you wanted to use the constructor, you’d have to use the following code:

button = new mx.controls.Button();

Obviously, it’s much more convenient to use the shorthand form of a class name (i.e., Button). ActionScript allows you to reference a class by the shorthand notation if you first add an import statement. An import statement tells the compiler that you can refer to the class by its shorthand notation from that point forward. The following is an import statement for the Button class:

import mx.controls.Button;

You can simply refer to Button as such from that point forward.

Note

If you import two Button classes (from different packages) in the same class, you must still refer to them using their fully qualified class names within that class.

Many developers understand how to reference packages using import statements, but they are uncertain as to how to go about creating package names for classes they might write. Although there is no right or wrong way to name packages, there is a strong convention. The convention is to use the reverse domain as the first part of the package name. For example, if you were to write classes for a company with a domain name of examplecompany.com, all classes would be in the com.example package or subpackages. The exception to this is when the classes transcend ownership by one company or organization. In such cases, the package name generally starts with a unique value that represents the project. For example, Flex classes exist within an mx package.

Typically, most classes are further organized into subpackages. For example, rather than placing a class directly into the com.example package, it generally makes more sense to further organize the class into a subpackage, such as com.example.ui, com.example.services, or com.example.utils. These subpackages simply help you to logically organize classes.

Declaring Classes

Next, let’s look at the basic syntax and structure of a class. At a minimum, all ActionScript 3.0 classes consist of the following elements:

  • Class package declaration

  • Class declaration

Additionally, classes almost always also have import statements.

Creating class files

Each class must be defined in its own file. (There are a few unique exceptions, but in most practical cases, a class must be defined in its own file.) The name of the file must be the same as the name of the class it contains, and the file must use the .as file extension. For instance, if you want to define an Example class, you must create a file named Example.as.

Package declarations

The syntax for all ActionScript 3.0 classes begins with a package declaration. As discussed earlier in this chapter, packages are used to organize classes. A package name in ActionScript corresponds to the directory structure within which the ActionScript file is stored. Each directory and subdirectory is delimited by a dot (.) in a package name. For example, if a class is stored in the example subdirectory of a com directory, the package name would be com.example. A class’s package declaration uses the package keyword followed by the package name. Opening and closing curly braces, which contain any import statements and class declarations, follow the package declaration. The following package declaration says that the enclosed class exists within the com.example package. This also means that the file must exist within a com/example directory relative to one of the source path directories.

package com.example {
  // Import statements go here.
  // Class declaration goes here.
}

Note

It’s considered a best practice to place all class files within packages, with the possible exception of main class files, when creating ActionScript 3.0-only (non-Flex) applications.

Import statements

As noted earlier, import statements should appear within the package declaration, but not within the class declaration. (Technically, import statements can be placed anywhere, but by convention, they should be placed within the package declaration, but not in the class declaration.) You must import any and all classes you intend to use. ActionScript 3.0 classes don’t automatically import classes. The following example imports the URLLoader and URLRequest classes from the Flash Player API:

package com.example {
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  // Class declaration goes here.
}

Class declaration

All public ActionScript 3.0 classes placed within package declarations must be declared using the public keyword, followed by the class keyword and the name of the class. (You’ll learn about the differences between public, private, protected, and internal classes in the next section.) Opening and closing curly braces then follow, within which you place the class definition. Class names always start with initial capital letters by convention. The next example declares an Example class in the com.example package.

package com.example {
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  public class Example {
    // Class code goes here.
  }
}

Get Programming Flex 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.