![]() | |
![]() | |
![]() | |
![]() |
Note: If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this section, because writing scripts using ActionScript 1 is deprecated; instead, see Creating Classes with ActionScript 2.0 for information on using ActionScript 2.0.
You can specify the object that a function is applied to and the parameter values that are passed to the function, using the call()
and apply()
methods of the Function object. Every function in ActionScript is represented by a Function object, so all functions support call()
and apply()
. When you create a custom class using a constructor function, or when you define methods for a custom class using a function, you can invoke call()
and apply()
for the function.
Note: If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this section, because writing scripts using ActionScript 1 is deprecated; instead, see Creating Classes with ActionScript 2.0 for information on using ActionScript 2.0.
The Function.call()
method invokes the function represented by a Function object.
In almost all cases, the function call operator (()
) can be used instead of the call()
method. The function call operator creates code that is concise and readable. The call()
method is primarily useful when the this
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, this
is set to myObject
, as in the following:
myObject.myMethod(1, 2, 3);
In some situations, you may want this
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)
For more information, see Function.call()
.
myFunction
.call(thisObject
,parameter1
, ...,parameterN
)
The method takes the following parameters:
thisObject
specifies the value of this
within the function body. parameter1
...,
parameterN
specify parameters to be passed to myFunction
. You can specify zero or more parameters. Note: If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this section, because writing scripts using ActionScript 1 is deprecated; instead, see Creating Classes with ActionScript 2.0 for information on using ActionScript 2.0.
The Function.apply()
method specifies the value of this
to be used within any function that ActionScript calls. This method also specifies the parameters to be passed to any called function.
The parameters are specified as an Array object. This is often useful when the number of parameters to be passed is not known until the script actually executes.
For more information, see Function.apply()
.
myFunction
.apply(thisObject
,argumentsObject
)
The method takes the following parameters:
thisObject
specifies the object that myFunction
is applied to.argumentsObject
defines an array whose elements are passed to myFunction
as parameters. ![]() | |
![]() | |
![]() | |
![]() |