ActionScript 3 Language Syntax Cheatsheet

Warning! Some information on this page is older than 5 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

Feb 2010

I started my Flash learning from reading about the ActionScript 3 language syntax from the "Learning ACTIONSCRIPT 3.0". Here is my quick-and-dirty sketch of a language syntax cheatsheet. I assume you are already familiar with programming in C++ and/or Java, so I emphasize differences.

Some background: ActionScript language comes from ECMAScript, which means it has syntax very similar to JavaScript, which in turn resembles C++ or Java. It's a case-sensitive language. It significantly evolved from version 1 through 2 until 3. It started as very dynamic, scripting-like language with dynamic data types and prototype-based object model (like in JavaScript or Lua). But now it looks more "serious" with the ability to define classes and compilation-time type checking (more like in C++ or Java).

Ending statements with semicolon ; is optional in ActionScript. Comments look exactly like in C++ - we have single line comments after // and multiline ones between /* and */. We also have many control statements looking just like in C++ - if, switch, while, do-while, return, break, for, continue. Interestingly, we can but labels into code like in C++ and then reference them in break and continue.

There are exceptions available, so we can use try-catch-finally blocks and throw exceptions like this:

throw new Error("Invalid email address");

Source code consists of packages with classes inside them, like:

package MyPackage1 {
  public class MyClass1 {

There can be only one public class inside a source file (default file extension is .as) and it has to have same name as the file name.

There can also be raw code (any statements, e.g. function calls) outside function definitions, like inside a class, a package or even at the top level. They are executed at program start.

Here is how you define variables (local variables or class fields):

var i1:int;
var s1:String = "abc";
var x:Number = 1, y:Number = 2.5;

You can also define constants:

const MyConst1:uint = 0xDEADC0DE;

Scoping rules for variables are similar to C++, so you can have static and normal class fields, as well as method parameters and local variables inside methods. There is a difference that {} blocks inside code do not create nested naming scope.

Primitive intrinsic data types are:

Complex intrinsic data types are: Array, XML, XMLList, Date, Function, RegExp, Object. Variables of these types are always passed and assigned by reference. Of course there is no manual memory management, the language has a garbage collector.

Variables are automatically initialized with default values. For int and uint it's 0, for Number it's NaN (!), for Boolean it's false, for Object and String it's null.

Here is how you define functions (methods). The example below is an event handler - events are widely used in Flash. There is an event for MOUSE_DOWN, MOUSE_MOVE, as well as ENTER_FRAME - called every frame, for example 30 times per second (sounds familiar? :)

private function OnMouseMove(event:MouseEvent):void

Default parameter values are supported, just like in C++ (and opposite to C#). There is also a rest operator ... available to enable passing any number of arguments.

Available attributes for classes are: internal (the default), public, dynamic (methods and properties can be added in runtime - see below), final (cannot be inherited). Attributes for class members are: internal (default), private, protected, public, static or a namespace name (see below).

Calling constructor of a base class can be done with super() keyword. If not explicitly called, the constructor of the base class is automatically called at the beginning of our constructor.

Methods do not need to be explicitly declared as virtual, but overriding them in subclasses requires override keyword. Method can also be declared as final so it cannot be overriden.

Because classes are organized inside packages, we have to either qualify them with full path like, or import them into global namespace with:

import*; // Importing everything from this package
import; // Importing single class

When it comes to operators, we have whole bunch of standard arithmetic, logical and bitwise operators available similar to C++ or Java:

String concatenation is + which is quite intuitive, but not so obvious (as PHP use dot . for this purpose and Lua uses double dot ..). Object-oriented operators are also available (as in every modern and comfortable language - C++ is not one of them :P), like "is" and "as". Operator "as" returns null if cast is not possible.

Explicit type casting is done this way: Int(myString1). These are actually global functions, not type casting operators. Casting string to a numeric type does parsing so characters are converted to number. Strings with numbers saved in hexadecimal form like "0x123F" are also supported. Casting Boolean to String returns "true" or "false". Casting String to Boolean returns false if string is null or empty "".

ActionScript supports interfaces. Thay are like classes, but have to use "interface" keyword instead of "class" and have some restrictions. Class inheritance is done with the "extends" keyword and subclassing interfaces with "implements" keyword.

ActionScript provides very nice syntax for declaring so called getters and setters - class properties that work like methods, but can be used like fields. Here is an example:

private var privateProperty:String;
public function get publicAccess():String {
  return privateProperty;
public function set publicAccess(setValue:String):void {
  privateProperty = setValue;

Enums have not explicit support in ActionScript. One should declare a class instead with static constants of type uint, String or any other.

public final class PrintJobOrientation
  public static const LANDSCAPE:String = "landscape";
  public static const PORTRAIT:String = "portrait";

Function in ActionScript is a type, so functions can be assigned to variables, passed and returned. They are closures, which means they remember the context from which they come, including local variables (for nested functions) or object properties (for class methods extracted from objects). Example:

var traceParameter:Function = function (aParam:String) { trace(aParam); }

Arrays can be initialized with [] parentheses, like:

var seasons:Array = ["spring", "summer", "autumn", "winter"];

Basic text printing to the debug output can be done with trace function, e.g.:

for (var i:uint = 0; i < 10; i++)

There are also two loop instructions to iterate over array elements or dynamic object properties. The for-in loop iterates over keys in array or property names in object:

var myObj:Object = {x:20, y:30};
for (var i:String in myObj)
  trace(i + ": " + myObj[i]);

The for-each loop iterates over values in array or property values in an object:

var myObj:Object = {x:20, y:30};
for each (var num:int in myObj)

The example above also shows dynamic object with arbitrary properties put inside during construction with {} operator. You can also dynamically add and delete properties of an existing object, like:

myObj.z = 10;
myObj['z'] = 10;
delete myObj.z;

To use your own class this way, you have to define it with dynamic keyword.

There is another dynamic feature still existent in ActionScript 3 - untyped (dynamically typed) variables. They are defined with asterisk * instead of type name.

var dynVar:*;
dynVar = 10;
dynVar = "Foo";

ActionScript has special syntax support for regular expressions with / operator, e.g.:

var myRegExp:RegExp = /foo-\d+/i; 
trace(myRegExp.exec("ABC Foo-32 def.")); // Foo-32

It looks very crazy for me that ActionScript accepts XML embedded just inside the code like an intrinsic type.

var employee:XML =

XML nodes can be addressed intuitively like in XPath, because language supports XML operators: @ . .. {} [] delete. For example:

var myXML:XML = <item id="42">
  <catalogName>Presta tube</catalogName>
trace(myXML.@id); // 42

Files (e.g. graphics or sound) can be embedded into classes with so called embedded asset classes, as in the example:

[Embed(source="sound1.mp3")] public var soundCls:Class;
public function SoundAssetExample()
  var mySound:SoundAsset = new soundCls() as SoundAsset;
  var sndChannel:SoundChannel =;

Packages in ActionScript work like namespaces in C++, but ActionScript also have namespaces. They do something completely different though and they are the most crazy thing I can see in the syntax of this language. They are the way to define your own class member visibility specifiers next to standard keywords like public, private, protected and internal. For example:

class Class1 {
  public namespace Namespace1;
  public function Method1():void { }
  Namespace1 function Method2():void { }

Comments | #flash Share


[Download] [Dropbox] [pub] [Mirror] [Privacy policy]
Copyright © 2004-2021