Adobe® Flex® 4 Language Reference
Hide Packages and Classes List |  Packages  |  Classes  |  Index  |  Appendixes
__Global__ 
Function 
PackageTop Level
Classpublic dynamic class Function
InheritanceFunction Inheritance Object

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 9, AIR 1.0

A function is the basic unit of code that can be invoked in ActionScript. Both user-defined and built-in functions in ActionScript are represented by Function objects, which are instances of the Function class.

Methods of a class are slightly different than Function objects. Unlike an ordinary function object, a method is tightly linked to its associated class object. Therefore, a method or property has a definition that is shared among all instances of the same class. Methods can be extracted from an instance and treated as "bound" methods (retaining the link to the original instance). For a bound method, the this keyword points to the original object that implemented the method. For a function, this points to the associated object at the time the function is invoked.

View the examples



Public Properties
 PropertyDefined By
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined By
  
apply(thisArg:*, argArray:*):*
Specifies the value of thisObject to be used within any function that ActionScript calls.
Function
  
call(thisArg:*, ... args):*
Invokes the function represented by a Function object.
Function
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Method Detail

apply

()method
AS3 function apply(thisArg:*, argArray:*):*

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 9, AIR 1.0

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

thisArg:* (default = NaN) — The object to which the function is applied.
 
argArray:* (default = NaN) — An array whose elements are passed to the function as parameters.

Returns
* — Any value that the called function specifies.

See also

call

()method 
AS3 function call(thisArg:*, ... args):*

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 9, AIR 1.0

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

thisArg:* (default = NaN) — An object that specifies the value of thisObject within the function body.
 
... args — The parameter or parameters to be passed to the function. You can specify zero or more parameters.

Returns
*

See also

FunctionExample.as

The following example uses the FunctionExample, SimpleCollection, EventBroadcaster, and EventListener classes to show various uses of functions in ActionScript. This is accomplished with the following steps:
  1. The constructor for FunctionExample creates a local variable named simpleColl, which is populated with an array of integers ranging from 1 to 8.
  2. The simpleColl object is printed using trace().
  3. An EventListener object, listener, is added to simpleColl.
  4. When the insert() and remove() functions are called, the listener responds to their events.
  5. A second SimpleCollection object is created named greaterThanFourColl.
  6. The greaterThanFourColl object is assigned the result of simpleColl.select() with the argument 4 and an anonymous function. The SimpleCollection object's select method is an internal iterator that uses the anonymous function parameter as a block.
package {
    import flash.display.Sprite;
    
    public class FunctionExample extends Sprite {
        public function FunctionExample() {
            var simpleColl:SimpleCollection;
            simpleColl = new SimpleCollection(0, 1, 2, 3, 4, 5, 6, 7, 8);
            trace(simpleColl);        // 0, 1, 2, 3, 4, 5, 6, 7, 8

            var listener:EventListener = new EventListener();
            simpleColl.addListener(listener);
            simpleColl.insert(9);        // itemInsertedHandler: 9
            simpleColl.remove(8);        // itemRemovedHandler: 8
            trace(simpleColl);        // 0, 1, 2, 3, 4, 5, 6, 7, 9

            var greaterThanFourColl:SimpleCollection;
            greaterThanFourColl = simpleColl.select(4, function(item:int, value:int){ return item > value });
            trace(greaterThanFourColl);    // 5, 6, 7, 9
        }
    }
}
    
import flash.display.Sprite;
    
class EventBroadcaster {
    private var listeners:Array;

    public function EventBroadcaster() {
        listeners = new Array();
    }
        
    public function addListener(obj:Object):void {
        removeListener(obj);
        listeners.push(obj);
    }
        
    public function removeListener(obj:Object):void {
        for(var i:uint = 0; i < listeners.length; i++) {
            if(listeners[i] == obj) {
                listeners.splice(i, 1);
            }
        }
    }
    
    public function broadcastEvent(evnt:String, ...args):void {
        for(var i:uint = 0; i < listeners.length; i++) {
            listeners[i][evnt].apply(listeners[i], args);
        }
    }    
}
    
class SimpleCollection extends EventBroadcaster {
    private var arr:Array;
        public function SimpleCollection(... args) {
        arr = (args.length == 1 && !isNaN(args[0])) ? new Array(args[0]) : args;
    }
        
    public function insert(obj:Object):void {
        remove(obj);
        arr.push(obj);
        broadcastEvent("itemInsertedHandler", obj);
    }
        
    public function remove(obj:Object):void {
        for(var i:uint = 0; i < arr.length; i++) {
            if(arr[i] == obj) {
                var obj:Object = arr.splice(i, 1)[0];
                broadcastEvent("itemRemovedHandler", obj);
            }
        }
    }

    public function select(val:int, fn:Function):SimpleCollection {
        var col:SimpleCollection = new SimpleCollection();
        for(var i:uint = 0; i < arr.length; i++) {
            if(fn.call(this, arr[i], val)) {
                col.insert(arr[i]);
            }
        }
        return col;
    }
        
    public function toString():String {
        var str:String = new String();
        for(var i:uint = 0; i < arr.length - 1; i++) {
            str += arr[i] + ", ";
        }
        str += arr[arr.length - 1];
        return str;
    }
}

class EventListener {
    public function EventListener() {
    }
    
    public function itemInsertedHandler(obj:Object):void {
        trace("itemInsertedHandler: " + obj);
    }
    
    public function itemRemovedHandler(obj:Object):void {
        trace("itemRemovedHandler: " + obj);        
    }
}