Function


Object   |   +-Function public dynamic class Function extends Object

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

Availability: ActionScript 1.0; Flash Player 6

Property summary

Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


Method summary

Modifiers

Signature

Description

 

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 (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)


apply (Function.apply method)

public apply(thisObject:Object, [argArray:Array]) : Void

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.

Availability: ActionScript 1.0; Flash Player 6

Parameters

thisObject:Object - The object to which myFunction is applied.

argArray:Array [optional] - An array whose elements are passed to myFunction as parameters.

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

See also

call (Function.call method)

call (Function.call method)

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

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.

Availability: ActionScript 1.0; Flash Player 6

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 -

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:

this == obj? true

See also

apply (Function.apply method)



ActionScript 2.0 Language Reference for Macromedia Flash 8
ActionScript 2.0 Language Reference for Macromedia Flash 8
ISBN: 0321384040
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net