Follow Flex After Dark on Twitter

ActionScript Language Overview

ActionScript is a powerful, object-oriented scripting language. The latest/current version of ActionScript is version 3.0.

ActionScript and MXML are the languages of Flex. ActionScript code is defined in files with the .as extension or in within <mx:Script> elements of .mxml files.

ActionScript is based on the ECMAScript standard.

Variables

ActionScript variables are strongly typed.

ActionScript variable definitions:

   var object:Object = new Object();
   var array:Array = new Array();
   var count:int = 1;
   var name:String = "Donnie Brasco";
   var names:Array = { "Alice", "Bob", "Carl" }; // short-hand array definition

ActionScript also supports using "*" (wildcard) as a variables type:

   var someObject:*;

The "*" wildcard type means any object type can be assigned to the variable. The difference between declaring the type as Object though is that a compiler won't complain about calling methods on the variable that aren't in Object (the compiler can't type checking on the variable).

var varName:VarType = new VarType();

ActionScript does support typeless variables, but it is generally not encouraged.

   var var1 = null; // un-typed variable
   var var2:* = null; // any-typed variable

Functions / Methods

TODO...

ActionScript function definition:

   public function doSomething( arg:Object ):String
   {
      return "something";
   }
   
   // another function with an optional argument
   public function anotherFunction( arg:Object, opt:String=null ):void
   {
      // do something
   }

public function functionName( argName:ArgType ):ReturnType

ActionScript also allows optional function arguments.

ActionScript function definition with optional function argument:

   public function anotherFunction( arg:Object, opt:String=null ):void
   {
      // do something
   }

In the above example, the opt argument is optional because it has a default value assigned in the function signature. Optional arguments must be declared after those that are required.

Read more about ActionScript Functions and supported Get/Set Functions for property accessors...

Classes and Interfaces

ActionScript requires classes and interfaces to be declared in a package and the explicit import of used classes/interfaces that are not in the same package. (T package name to be ommitted, representing the default package.)

Class Definition

ActionScript class definition:

package packageName
{
   import another.packageName.*;
   
   public class ClassName extends SuperClassName implements InterfaceName
   {
      public ClassName()
      {
         // constructor
      }
   }
}

ActionScript also supports dynamic classes...

Interface Definition

ActionScript interface definition:

package packageName
{
   public interface InterfaceName extends AnotherInterface
   {
      // public not necessary (and not allowed)
      function methodName():void;
   }
}

ActionScript Interfaces do not allow property definitions.

Casting and Runtime Type Checking

In strongly-typed languages, such as ActionScript, it is sometimes necessary to cast the reference of one typed variable to another. For example, if you have a Person object referenced just as Object (every object's super-type), but you need to get it cast as its more specific type.

For runtime type checking ActionScript uses the is operator.

ActionScript runtime type checking and casting:

   // cast Object to Person
   if( object is Person )
   {
      // note the lack of the new keyword
      var person:Person = Person( object );
   }

ActionScript also offers another method, similar to casting, using the as operator. The ActionScript as operator will cast the object and assign it the variable, but only if object is of the proper type (otherwise it assigns null).

ActionScript usage of the as operator:

   // use "as" to safely assign object as a Person or null
   var person:Person = object as Person;

Looping

ActionScript "for" and "for each" loops:

   // loop over an array
   for( var i:int = 0; i < array.length; i++ )
   {
      // use array[i]
   }
   
   // loop over a collection
   var item:Object;
   for each( item in collection )
   {
      // use item
   }

Exceptions

ActionScript throws exceptions and supports the try/catch/finally structure for handling them.

ActionScript exception handling using try/catch/finally:

   // functions do not declare that they throw
   public function doSomething():void
   {
      try
      {
         // try something
      }
      catch( error:Error )
      {
         // handle error by rethrowing
         throw error;
      }
      finally
      {
         // reached whether or not there's an exception
      }
   }

Learn more about ActionScript Exceptions and Handling...

ActionScript 3 Top-Level Data Types

ActionScript 3 Top-Level Data Types:

  • Array - The Array class lets you access and manipulate arrays.
  • Boolean - A Boolean object is a data type that can have one of two values, either true or false, used for logical operations.
  • Class - A Class object is created for each class definition in a program.
  • Date - The Date class represents date and time information.
  • Error - The Error class contains information about an error that occurred in a script.
  • Function - A function is the basic unit of code that can be invoked in ActionScript.
  • int - The int class lets you work with the data type representing a 32-bit signed integer.
  • Number - A data type representing an IEEE-754 double-precision floating-point number.
  • RegExp - The RegExp class lets you work with regular expressions, which are patterns that you can use to perform searches in strings and to replace text in strings.
  • String - The String class is a data type that represents a string of characters.
  • uint - The uint class provides methods for working with a data type representing a 32-bit unsigned integer.
  • XML - The XML class contains methods and properties for working with XML objects.
  • XMLList - The XMLList class contains methods for working with one or more XML elements.
  • (some have been omitted, see http://livedocs.adobe.com/flex/3/langref/package-detail.html for more)

Comparisons to other languages

Related Links:

Related Docs
  • ActionScript Exceptions ActionScript handles Exceptions using try, catch, and finally.
  • ActionScript Functions ActionScript Functions are first-class objects and can be registered as event listeners.
  • MXML Language Overview of the MXML programming language used by Flex.
  • Object-Oriented ActionScript ActionScript is a full-featured Object-Oriented language with support for classes, interfaces, encapsulation, polymorphism, and inheritance.
  • ActionScript vs Java ActionScript has many similarities to the Java programming language.
Recent Docs
Random Docs