User Guide

94 ActionScript Language and Syntax
Function statements provide a more consistent experience between the two compiler modes in
that you can use dot syntax in both strict and standard mode to invoke a method declared
using a function statement. This is not necessarily true for methods declared with a function
expression. For example, the following code defines a class named Example with two
methods:
methodExpression(), which is declared with a function expression, and
methodStatement(), which is declared with a function statement. In strict mode, you cannot
use dot syntax to invoke the
methodExpression() method.
class Example
{
var methodExpression = function() {}
function methodStatement() {}
}
var myEx:Example = new Example();
myEx.methodExpression(); // error in strict mode; okay in standard mode
myEx.methodStatement(); // okay in strict and standard modes
Function expressions are considered better suited to programming that focuses on run-time,
or dynamic, behavior. If you prefer to use strict mode, but also need to call a method declared
with a function expression, you can use either of two techniques. First, you can call the
method using square brackets (
[]) instead of the dot (.) operator. The following method call
succeeds in both strict mode and standard mode:
myExample["methodLiteral"]();
Second, you can declare the entire class as a dynamic class. Although this allows you to call the
method using the dot operator, the downside is that you sacrifice some strict mode
functionality for all instances of that class. For example, the compiler does not generate an
error if you attempt to access an undefined property on an instance of a dynamic class.
There are some circumstances in which function expressions are useful. One common use of
function expressions is for functions that are used only once and then discarded. Another, less
common use is for attaching a function to a prototype property. For more information, see
“The prototype object” on page 144.