
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
192
|
Chapter 9: Functions
// Store a function literal in a variable
var mouseCoords = function () { return [ _xmouse, _ymouse ]; };
// Now we can execute the function
mouseCoords();
Note that because ActionScript does not support JavaScript’s Function( ) construc-
tor, dynamic functions cannot be composed at runtime, as they can in JavaScript.
As we’ll see in Chapter 12, function literals are often used when assigning methods
to classes or objects. For example:
Ball.prototype.startMoving = function ( ) {
// Movement code goes here
};
is the same as:
Ball.prototype.startMoving = startMoving;
function startMoving ( ) {
// Movement code goes here
}
However, there are three subtle differences between function literals and standard
function declarations:
• When assigned as a method, a function literal does not leave an extraneous func-
tion reference defined in the scope of the function declaration. For example, in
the first Ball example (assuming the Ball code is attached to the main timeline), a
function literal is stored in
Ball.prototype.startMoving and no separate func-
tion reference remains defined on
_root. But in the second example, a reference
to startMoving( ) exists both in
Ball.prototype.startMoving and on _root.
• A standard function declaration does not end in a semicolon, whereas a