start page | rating of books | rating of authors | reviews | copyrights

Book HomeActionScript: The Definitive GuideSearch this book

9.5. Function Literals

ActionScript allows us to create function literals, which are convenient when we need a function temporarily or when we'd like to use a function where an expression is expected.

Function literals have the same syntax as standard function declarations except that the function name is omitted and there's a semicolon after the statement block. The general form is:

function (param1, param2, ... paramn) { statements };

where param1,param2,...paramn is an optional list of parameters and statements is one or more statements that constitute the function body. Because it doesn't include a function name, a function literal is "lost" unless we store it in a variable (or an array element or object property). We can store a function literal in a variable for later access, like this:

// 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( ) constructor, dynamic functions cannot be composed at runtime, as they can in JavaScript.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.