Using Function object properties in ActionScript 1.0 801
Using Function object properties in
ActionScript 1.0
You can specify the object to which a function is applied 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.
Invoking a function using the Function.call() method
in ActionScript 1.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 shown in the following example:
myObject.myMethod(1, 2, 3);
In some situations, you might want this to point somewhere else; for instance, if a function
must be invoked as a method of an object but is not actually stored as a method of that object,
as shown in the following example:
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)
NOTE
Many Flash users can greatly benefit from using ActionScript 2.0, especially with
complex applications. For information on using ActionScript 2.0, see Chapter 7,
“Classes,” on page 225.
NOTE
Many Flash users can greatly benefit from using ActionScript 2.0, especially with
complex applications. For information on using ActionScript 2.0, see Chapter 7,
“Classes,” on page 225.