Monday, December 3, 2012

Nested functions

You can nest functions, which means that functions can be declared within other functions. A nested function is only available within its parent function unless a reference to the function is passed to external code. For example, the following code declares two nested functions inside the getNameAndVersion() function:
function getNameAndVersion():String {
     function getVersion():String {
     return "8.5";
}
function getProductName():String {
     return "Flash Player";
}
return (getProductName() + " " + getVersion());
}
trace(getNameAndVersion()); // Output: Flash Player 8.5
When nested functions are passed to external code, they are passed as function closures, which
means that the function retains any definitions that are in scope when the function is defined.
For more information, see “Function closures” on page 83.
Programming Actionscript 3. Powered by Blogger.