ActionScript 3.0 Language and Components Reference | All Packages | All Classes | Language Elements | Index | Appendixes | Conventions | Frames No Frames |
Function

Package | Top Level
Class | public dynamic class Function
Inheritance | Function Object

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

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

See also

Methods

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | prototype : Object [static] A reference to the prototype object of a class or function object. | Object

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | apply(thisObject:Object, argArray:Array = null):void Specifies the value of thisObject to be used within any function that ActionScript calls. | Function
|  | call(thisObject:Object, parameter1:String = null):void Invokes the function represented by a Function object. | Function
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Method Detail

apply | () | method

`AS3 function apply(thisObject:Object, argArray:Array = null):void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

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

| `thisObject:Object` — The object to which the function is applied.
| `argArray:Array` (default = `null`)`` — An array whose elements are passed to the function as parameters.

See also

Function.call()

call | () | method |

`AS3 function call(thisObject:Object, parameter1:String = null):void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

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

| `thisObject:Object` — An object that specifies the value of `thisObject` within the function body.
| `parameter1:String` (default = `null`)`` — A parameter to be passed to the function. You can specify zero or more parameters.

See also

Function.apply()
Examples How to use examples
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);
        }
    }

Submit Feedback on LiveDocs
© 2004-2008 Adobe Systems Incorporated. All rights reserved.
Tue May 20 2008, 04:02 AM -07:00