PackageTop Level
Classpublic dynamic class Function
InheritanceFunction Inheritance Object

Player version: Flash Player 6

Both user-defined and built-in functions in ActionScript are represented by Function objects, which are instances of the Function class.



Public Methods
 Method
  
apply(thisObject:Object, [argArray:Array]):Void
Specifies the value of thisObject to be used within any function that ActionScript calls.
  
call(thisObject:Object, [parameter1:Object]):Object
Invokes the function represented by a Function object.
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Method detail
apply()method
public function apply(thisObject:Object, [argArray:Array]):Void

Player version: Flash Player 6

Specifies the value of thisObject to be used within any function that ActionScript calls. This method also specifies the parameters to be passed to any called function. Because apply() is a method of the Function class, it is also a method of every Function object in ActionScript.

The parameters are specified as an Array object, unlike Function.call(), which specifies parameters as a comma-delimited list. This is often useful when the number of parameters to be passed is not known until the script actually executes.

Returns the value that the called function specifies as the return value.

Parameters
thisObject:Object — The object to which myFunction is applied.
 
argArray:Array [optional] — An array whose elements are passed to myFunction as parameters.

See also


Example
The following function invocations are equivalent:
Math.atan2(1, 0)
Math.atan2.apply(null, [1, 0])

The following simple example shows how apply() passes an array of parameters:

function theFunction() {
    trace(arguments);
}

// create a new array to pass as a parameter to apply()
var firstArray:Array = new Array(1,2,3);
theFunction.apply(null,firstArray);
// outputs: 1,2,3

// create a second array to pass as a parameter to apply()
var secondArray:Array = new Array("a", "b", "c");
theFunction.apply(null,secondArray);
// outputs a,b,c

The following example shows how apply() passes an array of parameters and specifies the value of this:

// define a function 
function theFunction() {
    trace("this == myObj? " + (this == myObj));
    trace("arguments: " + arguments);
}

// instantiate an object
var myObj:Object = new Object();

// create arrays to pass as a parameter to apply()
var firstArray:Array = new Array(1,2,3);
var secondArray:Array = new Array("a", "b", "c");

// use apply() to set the value of this to be myObj and send firstArray
theFunction.apply(myObj,firstArray);
// output: 
// this == myObj? true
// arguments: 1,2,3

// use apply() to set the value of this to be myObj and send secondArray
theFunction.apply(myObj,secondArray);
// output: 
// this == myObj? true
// arguments: a,b,c

call()method 
public function call(thisObject:Object, [parameter1:Object]):Object

Player version: Flash Player 6

Invokes the function represented by a Function object. Every function in ActionScript is represented by a Function object, so all functions support this method.

In almost all cases, the function call (()) operator can be used instead of this method. The function call operator produces code that is concise and readable. This method is primarily useful when the thisObject parameter of the function invocation needs to be explicitly controlled. Normally, if a function is invoked as a method of an object, within the body of the function, thisObject is set to myObject, as shown in the following example:

myObject.myMethod(1, 2, 3);

In some situations, you might want thisObject to point somewhere else; for example, if a function must be invoked as a method of an object, but is not actually stored as a method of that object:

myObject.myMethod.call(myOtherObject, 1, 2, 3); 

You can pass the value null for the thisObject parameter to invoke a function as a regular function and not as a method of an object. For example, the following function invocations are equivalent:

Math.sin(Math.PI / 4)
Math.sin.call(null, Math.PI / 4)

Returns the value that the called function specifies as the return value.

Parameters
thisObject:Object — An object that specifies the value of thisObject within the function body.
 
parameter1:Object [optional] — A parameter to be passed to the myFunction. You can specify zero or more parameters.

Returns
Object

See also


Example
The following example uses Function.call() to make a function behave as a method of another object, without storing the function in the object:
function myObject() {
}
function myMethod(obj) {
    trace("this == obj? " + (this == obj));
}
var obj:Object = new myObject();
myMethod.call(obj, obj);

The trace() statement displays:

The trace() statement sends the following code to the log file:

this == obj? true