Functions in ActionScript 2.0 are the functions of ActionScript 1.0 with optional types on their parameters and results. In class files they are methods; in FLA scripts and include files they are also free-standing functions and function literals, as before.
Declaring functions
| Form | Example |
|---|---|
| Method, in a class | public function area():Number { return w * h; } |
| Declaration, in an FLA script | function clamp(n:Number, lo:Number, hi:Number):Number { … } |
| Function literal | var double:Function = function (n:Number):Number { return n * 2; }; |
| Handler property | start_btn.onRelease = function ():Void { … }; |
- A parameter or result without a type is unchecked.
Voidas the return type says the function returns nothing; areturnwith a value is then an error.- There are no default parameter values and no rest parameters. A caller may still
pass fewer arguments, which are
undefined, or more, which are only inarguments. - There is no overloading: a class has one method of each name, and a subclass method of the same name overrides it.
- The type of a function value is
Function; its parameter types are not part of it.
The arguments object
| Member | Is |
|---|---|
arguments[i], arguments.length | The arguments passed, from 0, and their number |
arguments.callee | The function being run |
arguments.caller | The function that called it |
Methods as callbacks
A method stored as a handler or passed to another object loses its object: when it
runs, this is whatever object calls it, such as the button for an
onRelease handler. Inside a class, members compiled as
this.name then point at the wrong object. Three ways round this were
used:
import mx.utils.Delegate;
class com.example.Player {
private var play_btn:Button;
private var playing:Boolean = false;
public function Player(button:Button) {
play_btn = button;
// 1. Delegate: a function that calls toggle with this set to the Player
play_btn.onRelease = Delegate.create(this, toggle);
// 2. A local variable kept by a nested function
var owner:Player = this;
play_btn.onRollOver = function () { owner.hint(); };
// 3. setInterval's object form
setInterval(this, "tick", 1000);
}
private function toggle():Void { playing = !playing; }
private function hint():Void { trace("Play or pause"); }
private function tick():Void { if (playing) trace("tick"); }
}
this when a method is called back.Delegate.create(scope, method) comes with Flash from the Flash MX 2004
7.2 update. It is an ordinary class, compiled into each movie that uses it. It passes
on the callback’s arguments but not its original this.
Functions as objects
| Member | Does |
|---|---|
f.call(thisObject, arg1, …) | Calls the function with this set. |
f.apply(thisObject, argsArray) | The same, with the arguments in an array. |
f.prototype | The prototype of objects the function makes with new; for a class, where its methods are. |
A class is itself a function, its constructor, so typeof Player is
"function".
Limits
- The player stops a script that recurses more than 256 calls deep.
- A script that runs longer than the time limit, 15 seconds by default, makes the player offer to stop it.
- From Flash Player 7, functions compiled for that player keep their local
variables, arguments and
thisin fast registers, and run faster than those of earlier versions; see Compiler.