Functions came to Flash with Flash 5. Before it, the nearest thing was Flash 4’s
call(frame), which ran the script of another frame. In ActionScript 1.0 a
function is an object that can be stored, passed and given properties, and functions are
also how objects and classes are made.
Declaring functions
| Form | Example | Notes |
|---|---|---|
| Declaration | function area(w, h) { return w * h; } | A variable of the timeline it is on. It exists from the start of its frame’s script, so code above it in the same script can call it. |
| Function literal | area = function (w, h) { return w * h; }; | An anonymous function, stored when the assignment runs; earlier code cannot call it. Ends with a semicolon. |
| Method | ball.move = function (dx) { this._x += dx; }; | A function literal stored as a property of an object or clip. |
A function declared on a timeline is reached from elsewhere by its path, as
_root.area(3, 4). It lasts as long as its clip: once the clip is removed,
calls to it do nothing. Functions meant for the whole movie therefore go on the main
timeline, or from Flash MX on _global. ActionScript 1.0 has no
new Function("code"): functions can only be written in the source.
Parameters and arguments
A function can be called with any number of arguments, whatever its parameter list.
Parameters with no argument are undefined. Every argument, named or not,
is in the arguments object:
| Member | Is | Player |
|---|---|---|
arguments[i] | The argument at position i, from 0 | 5 |
arguments.length | The number of arguments passed | 5 |
arguments.callee | The function being run, for recursion in an anonymous function | 5 |
arguments.caller | The function that called this one | 6 |
function sum() {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
trace(sum(1, 2, 3)); // 6
Primitive arguments are copied; objects, arrays and clips are passed by reference, so a function can change them.
Scope and nested functions
Inside a function, names are looked up first among its parameters and
var variables, then on the timeline where the function was defined, then
globally; see Variables and scope.
A name assigned without var inside a function becomes a variable of that
timeline, a common source of mistakes.
A function defined inside another can use the outer function’s local variables. In
Flash MX this gave the usual way to reach an object from a handler, whose
this is the clip or button the handler belongs to:
function Gallery(clip) {
var owner = this;
this.clip = clip;
this.index = 0;
clip.next_btn.onRelease = function () {
owner.show(owner.index + 1); // this is the button here
};
}
this
- Called as a method,
obj.f(), a function’sthisisobj. - Called with
new,thisis the new object; see Objects and prototypes. - Run as a handler property such as
onPress,thisis the clip or button; as a listener method, the listener object. - In a frame script,
thisis the timeline. - From Flash MX,
f.call(obj, a, b)andf.apply(obj, [a, b])run a function with a chosenthis.
Functions as objects
A function is an object of type "function". It can be stored in
variables, arrays and properties, passed as an argument and returned. It can carry
properties of its own, and every function has a prototype property,
used when it is called with new.
| Member | Does | Player |
|---|---|---|
f.call(thisObject, arg1, …) | Calls the function with this set and the arguments listed. | 6 |
f.apply(thisObject, argsArray) | Calls it with the arguments taken from an array, such as another function’s arguments. | 6 |
f.prototype | The object that objects made with new f() inherit from. | 5 |
Limits
- Recursion is allowed. The player stops a script that goes more than 256 calls deep, with the message that 256 levels of recursion were exceeded.
- A script that runs too long, 15 seconds by default, makes the player ask whether
to stop it. Long work is spread across frames, or from Flash MX across
setIntervalcalls.