ActionScript Syntax for Flex Applications (Page 1 of 4 )
In this second part of a five-part series that focuses on using ActionScript in Flex applications, you will learn ActionScript's syntax and how to use it with Flex. This article is excerpted from chapter four of the book
Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
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 oneButton class within a scope. If you tried to declare twoButtonclasses 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, theButtonclass that’s part of the Flex framework (i.e., the button UI component) exists within a package calledmx.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 forButtonismx.controls.Button. That ensures that if you want to create anotherButtonclass in a different package, you can do so without conflicting withmx.controls.Button. For example,mx.controls.Buttonandcom.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 a class by its fully qualified name. For example, if you want to declare aButtonvariable, 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 animportstatement. Animportstatement tells the compiler that you can refer to the class by its shorthand notation from that point forward. The following is animportstatement for theButton class:
import mx.controls.Button;
You can simply refer toButtonas such from that point forward.
If you import twoButton classes (from different packages) in the same class, you must still refer to them using their fully qualified class names within that class.
Next: Declaring Classes >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|