O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functions as Values

In ActionScript, every function is represented by an instance of the Function class. As such, a function can be assigned to a variable, passed to a function, or returned from a function, just like any other value. For example, the following code defines a function, a( ) and then assigns it to the variable b. Notice that the parentheses operator, ( ), is omitted; if it were included, the code would simply assign a( )'s return value to b.

function a () {
}
var b = a;

Once a function has been assigned to a variable, it can be invoked through that variable using the standard parentheses operator, ( ). For example, the following code invokes the function a( ) through the variable b:

b();

Function values are typically used when creating dynamic classes and objects, as discussed in the "Dynamically Adding New Behavior to an Instance" and "Using Prototype Objects to Augment Classes" in Chapter 15.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required