Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 103

Programming actionscript 3.0
Table of Contents

Advertisement

Although it may seem strange to programmers new to ActionScript, functions can have
properties and methods, just as any other object can. In fact, every function has a read-only
property named
length
different from the
arguments.length
to the function. Recall that in ActionScript, the number of arguments sent to a function can
exceed the number of parameters defined for that function. The following example, which
compiles only in standard mode because strict mode requires an exact match between the
number of arguments passed and the number of parameters defined, shows the difference
between the two properties:
function traceLength (x:uint, y:uint):void
{
trace("arguments received: " + arguments.length);
trace("arguments expected: " + traceLength.length);
}
traceLength(3, 5, 7, 11);
/* output:
arguments received: 4
arguments expected: 2 */
You can define your own function properties by defining them outside your function body.
Function properties can serve as quasi-static properties that allow you to save the state of a
variable related to the function. For example, you may want to track the number of times a
particular function is called. Such functionality could be useful if you are writing a game and
want to track the number of times a user uses a specific command, although you could also
use a static class property for this. The following code creates a function property outside the
function declaration and increments the property each time the function is called:
someFunction.counter = 0;
function someFunction():void
{
someFunction.counter++;
}
someFunction();
someFunction();
trace(someFunction.counter); // 2
that stores the number of parameters defined for the function. This is
property, which reports the number of arguments sent
Functions
103

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents