Function

private class
system.Function

Description

A Function is a closure primitive. It captures statements, arguments, and a referencing environment for execution.

Functions are typically invoked with parenthesis operators ():

var f:Function = function(x:Number):Number { return x + 1; };
f(1); // -> 2

The call() and apply() methods can also be used when the calling object needs to be specified.

As a first-class data type, a function can be assigned to variables and returned from functions:

var f1:Function = function(x:Number, y:Number):Number { return x + y; };
var f2:Function = f1;
f1(3,4); // -> 7
f2(5,6); // -> 11
var g:Function = function(n:Number):Function {
    return function(x:Number) { return x + n; }
};
var skipTwo:Function = g(2);
skipTwo(7); // -> 9
skipTwo(8); // -> 10

See also:
#call()
#apply()

API overview

Constructor

Function ()

Attributes

Public attributes

length: Number read-only

The number of formal arguments expected by the function

Functions

Public functions

apply ( thisArg: Object = null , args: Vector.<Object> = null ): Object native

Invokes the function with a context and vector of arguments

call ( thisArg: Object = null , rest: Vector.<Object> ): Object native

Invokes the function with a context and comma-delimited set of arguments

Constructor

Function ()

Attributes

length: Number

read-only

The number of formal arguments expected by the function. This does not include use of the ...rest parameter.

Examples:

trace((function()        {}).length); // -> 0
trace((function(a)       {}).length); // -> 1
trace((function(a, b)    {}).length); // -> 2
trace((function(...args) {}).length); // -> 0, rest param not counted

Functions

apply ( thisArg: Object = null , args: Vector.<Object> = null ): Object

native

Invokes the function with a context and vector of arguments.

apply() is very similar to call(); the difference is how they accept arguments. apply() expects them bundled in a Vector:

var args:Vector.<Object> = [arg1, arg2, arg3];
myFunc.apply(null, args);

Parameters

thisArg: Object = null Value to use as the calling object, i.e. an instance for the method to be called on or null in the case of static/anonymous functions
args: Vector.<Object> = null Vector of arguments to pass into the function.

Returns

Object The Object returned by the called function.

call ( thisArg: Object = null , rest: Vector.<Object> ): Object

native

Invokes the function with a context and comma-delimited set of arguments.

call() is very similar to apply(); the difference is how they accept arguments. call() expects them individually:

myFunc.call(null, arg1, arg2, arg3);

Parameters

thisArg: Object = null Value to use as the calling object, i.e. an instance for the method to be called on or null in the case of static/anonymous functions
rest: Vector.<Object> Zero or more arguments to pass into the function.

Returns

Object The Object returned by the called function.

getFullTypeName (): String
Inherited from Object

native

Gets the fully qualified type name of the Object. The fully qualified type name includes the package of the type.


Returns

String fully qualified type name of the Object.

getType (): Type
Inherited from Object

native

Gets the Type that describes the Object.


Returns

Type The Type that describes the object.

getTypeName (): String
Inherited from Object

native

Gets the type name of the Object.


Returns

String type name of the Object.

toString (): String
Inherited from Object

native

Returns a String that describes the Object. This can be overriden to provide extra details when printing objects using trace().


Returns

String String that described the Object.

: