The following lines are taken from the specification document :
/**********************************************************************************
Function objects have a property named prototype
whose value is used to initialize the intrinsic delegate property of the objects it creates. The prototype
property has a default value of a new instance of the class Object. Building on the previous example:
function A() { this.x = 10 }
function B() {}
B.prototype = new A
var o = new B
trace(o.x) // traces 10
The value of o
is an instance of B,
which delegates to an instance of A
, which has a property named x
with value of 10.
Constructor methods inside of a class are also used to create objects. But, unlike constructor functions, constructor methods create objects with a set of fixed properties (traits) associated with its class and a delegate that is also an instance of its class.
class A {
var x
function A() { this.x = 10 }
}
var o = new A
trace(o.x) // traces 10
There are some subtle differences between the preceding example and the one involving a function constructor:
x
is a fixed property of each instance ofA
rather than a dynamic property.A.prototype
is an instance ofA
rather than an instance of Object.- The expression
A(expr)
does not call the functionA
defined in classA
. It results in an explicit conversion of the value ofexpr
to the typeA
.
Class methods are functions that are defined with the static
attribute inside of a class definition. A class method cannot be used as a constructor and does not define the this
reference. Class methods are in the scope of the class object in which they are defined.
Instance methods are functions that are defined without the static
attribute and inside a class definition. Instance methods are associated with an instance of the class in which they are defined. Instance methods can override or implement inherited class or interface methods and always have a value bound to this
.
this
in an instance method is the value of the instance the method belongs to.************************************************************************************/Having said that, consider the example below : (The examples below have syntax errors, i had to do so, r else the page would not display these tags properly, please correct them, if you r copy/pasting)
mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
mx:Button id="ti" click="getText()" label="uhuh"/>
mx:Script>
ADD CDATA TAG HERE WHEN COMPILING
import mx.core.UIComponent;
import mx.controls.Alert;
private var global_var:String = "global_var";
private function getText():void
{
Alert.show("Level 1 : "+global_var+"\n\n"+this.ti.label);
var inner1:Function = function():void
{
Alert.show("Level 2 : "+global_var+"\n\n"+this.ti.label)
var inner2:Function = function():void
{
Alert.show("Level 3 : "+global_var+"\n\n"+this.ti.label);
}
inner2();
};
inner1();
}
]]>
/mx:Script>
/mx:Application>
In the above example, getText() is a instance method, and the flex compiler sets this method's this reference to the Application instance. So, Level 1 is good, but the functions inner2 and inner3 are not instance methods and their this reference is not set to anything, we have to set it.
And how do we do it? We do it by calling the function, specifying the prototype object/target object for that function using call method. Below is a working example.
mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
mx:Button id="ti" click="getText()" label="uhuh"/>
mx:Script>
ADD CDATA TAG HERE WHEN COMPILING
import mx.core.UIComponent;
import mx.controls.Alert;
private var global_var:String = "global_var";
private function getText():void
{
Alert.show("Level 1 : "+global_var+"\n\n"+this.ti.label);
var inner1:Function = function():void
{
Alert.show("Level 2 : "+global_var+"\n\n"+this.ti.label)
var inner2:Function = function():void
{
Alert.show("Level 3 : "+global_var+"\n\n"+this.ti.label);
}
inner2.call(this);
};
inner1.call(this);
}
]]>
/mx:Script>
/mx:Application>
Notice inner2.call(this) and inner1.call(this)? But how come the inner functions access global_var variable, any explanation? In the Adobe Documentation, programming_actionscript3.pdf, Under section "Understanding variable scope", these lines are specified :
/*******************************************************************************
ActionScript variables, unlike variables in C++ and Java, do not have block-level scope. A
block of code is any group of statements between an opening curly brace ( { ) and a closing
curly brace ( } ). In some programming languages, such as C++ and Java, variables declared
inside a block of code are not available outside that block of code. This restriction of scope is
called block-level scope, and does not exist in ActionScript. If you declare a variable inside a
block of code, that variable will be available not only in that block of code, but also in any
other parts of the function to which the code block belongs.
*******************************************************************************/
The above answers our question, but the specification talks nothing about variable scopes. Its a relief to know there is a neat solution for the nested functions problem.