May
8
2009
AS3.0 TutBits #03: Nested functions
Nested functions are functions which are defined inside a method or in other function. They are only accessible in the method/function in which they are defined. Once there is a definition of a function inside a method it does not matter if you call that nested function before its definition or after it. It works in both cases.
Here is a short example of a nested function.
private function doCalculation():void { for (var i:uint; i < 10; i++) { // Do some serious calculation nestedLog("Calculation " + i + " done"); } // Nested function function nestedLog(logTxt:String):void { trace(logTxt); } }
The function defined in line 10 is only accessible in the scope of the method doCalculation(). Therefore, it is illegal to add any control access modifiers (public, private, protected) to the function.
Nested functions may be used to store some method-specific subroutines.
