Package | Top Level |
Class | public final class Function |
Inheritance | Function Object |
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
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.
Property | Defined by | ||
---|---|---|---|
constructor : Object
A reference to the class object or constructor function for a given object instance.
| Object | ||
declaration : String
[read-only]
Returns a string representing the function declaration from the source code.
| Function | ||
prototype : Object
[static]
A reference to the prototype object of a class or function object.
| Object |
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 |
declaration | property |
declaration:String
[read-only]Returns a string representing the function declaration from the source code.
This can be used to find out naming and parameter details, and also determine what kind of function the object is (anonymous function vs member function, etc).
Implementation AS3 function get declaration():String
apply | () | method |
AS3 function apply(thisArg:*, argArray:*):*
Language version: | ActionScript 3.0 |
Runtime version: | 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.
ParametersthisArg:* — The object to which the function is applied.
|
|
argArray:* — An array whose elements are passed to the function as parameters.
|
* — Any value that the called function specifies.
|
See also
call | () | method |
AS3 function call(thisArg:*, ... args):*
Language version: | ActionScript 3.0 |
Runtime version: | 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.
ParametersthisArg:* — 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.
|
* |
See also
FunctionExample
,
SimpleCollection
, EventBroadcaster
, and EventListener
classes
to show various uses of functions in ActionScript. This is accomplished with the following steps:
FunctionExample
creates a local variable named
simpleColl
, which is populated with an array of integers ranging from 1
to
8
. simpleColl
object is printed using trace()
.listener
, is added to simpleColl
.insert()
and remove()
functions are called, the listener responds
to their events.greaterThanFourColl
.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); } }