Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 116

Programming actionscript 3.0
Table of Contents

Advertisement

Methods are defined using the
the following:
public function sampleFunction():String {}
Or you can use a variable to which you assign a function expression, as follows:
public var sampleFunction:Function = function () {}
In most cases you will want to use a function statement instead of a function expression for
the following reasons:
Function statements are more concise and easier to read.
Function statements allow you to use the
information, see
"Overriding methods" on page
Function statements create a stronger bond between the identifier—that is, the name of
the function—and the code within the method body. Because the value of a variable can
be changed with an assignment statement, the connection between a variable and its
function expression can be severed at any time. Although you can work around this issue
by declaring the variable with
best practice because it makes the code hard to read and prevents the use of the
and
keywords.
final
One case in which you must use a function expression is when you choose to attach a function
to the prototype object. For more information, see
Constructor methods
Constructor methods, sometimes simply called constructors, are functions that share the same
name as the class in which they are defined. Any code that you include in a constructor
method is executed whenever an instance of the class is created with the
example, the following code defines a simple class named Example that contains a single
property named
status
function.
class Example
{
public var status:String;
public function Example()
{
status = "initialized";
}
}
var myExample:Example = new Example();
trace (myExample.status); // output: initialized
116
Object-Oriented Programming in ActionScript
keyword. You can use a function statement, such as
function
instead of
const
. The initial value of the
and
override
final
134.
, such a technique is not considered a
var
"The prototype object" on page
variable is set inside the constructor
status
keywords. For more
override
144.
keyword. For
new

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents