390 Chapter 12: ActionScript Dictionary
Example
The following function invocations are equivalent:
Math.atan2(1, 0)
Math.atan2.apply(null, [1, 0])
You could construct a SWF file that contains input entry fields that permit the user to enter the
name of a function to invoke, and zero or more parameters to pass to the function. Pressing a
“Call” button would then use the
apply method to call the function, specifying the parameters.
In this example, the user specifies a function name in an input text field called
functionName.
The number of parameters is specified in an input text field called
numParameters. Up to 10
parameters are specified in text fields called
parameter1, parameter2, up to parameter10.
on (release) {
callTheFunction();
}
...
function callTheFunction()
{
var theFunction = eval(functionName.text);
var n = Number(numParameters);
var parameters = [];
for (var i = 0; i < n; i++) {
parameters.push(eval("parameter" + i));
}
theFunction.apply(null, parameters);
}
Function.call()
Availability
Flash Player 6.
Usage
myFunction.call(thisObject, parameter1, ..., parameterN)
Parameters
thisObject
Specifies the value of this within the function body.
parameter1 A parameter to be passed to the myFunction. You can specify zero or more
parameters.
parameterN
Returns
Nothing.
Description
Method; invokes the function represented by a Function object. Every function in ActionScript is
represented by a Function object, so all functions support this method.