Package | Top Level |
Class | public dynamic class Function |
Inheritance | Function Object |
Language Version: | ActionScript 3.0 |
Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4 |
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.
More examples
Method | Defined By | ||
---|---|---|---|
Specifies the value of thisObject to be used within any function that ActionScript calls. | Function | ||
Invokes the function represented by a Function object. | Function | ||
Indicates whether an object has a specified property defined. | Object | ||
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | ||
Indicates whether the specified property exists and is enumerable. | Object | ||
Sets the availability of a dynamic property for loop operations. | Object | ||
Returns the string representation of this object, formatted according to locale-specific conventions. | Object | ||
Returns the string representation of the specified object. | Object | ||
Returns the primitive value of the specified object. | Object |
apply | () | method |
AS3 function apply(thisArg:*, argArray:*):*
Language Version: | ActionScript 3.0 |
Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4 |
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.
|
* — Any value that the called function specifies.
|
Related API Elements
call | () | method |
AS3 function call(thisArg:*, ... args):*
Language Version: | ActionScript 3.0 |
Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4 |
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.
|
* |
Related API Elements
FunctionExample
,
SimpleCollection
, EventBroadcaster
, and EventListener
classes
to show various uses of functions in ActionScript. This is accomplished with the following steps:
- The constructor for
FunctionExample
creates a local variable namedsimpleColl
, which is populated with an array of integers ranging from1
to8
. - The
simpleColl
object is printed usingtrace()
. - An EventListener object,
listener
, is added tosimpleColl
. - When the
insert()
andremove()
functions are called, the listener responds to their events. - A second SimpleCollection object is created named
greaterThanFourColl
. - The
greaterThanFourColl
object is assigned the result ofsimpleColl.select()
with the argument4
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); } }
Mon Nov 28 2011, 06:48 AM -08:00