Ted Patrick - Flash Platform @ Adobe Systems


Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



Simple Method Closure in AS3

DIGG IT!     5 Comments Published Friday, January 15, 2010 at 3:23 PM .

Method closures allow you to bind variables into the scope of an anonymous function. Watch the value of local variable 'i' in the example below in the returned anonymous function. It is a bit twisted but results show the scope of local variable 'i' is bound in the returned function from newCounter.

CODE AT PASTIE

package
{
import flash.display.Sprite;

public class ClosureAS3 extends Sprite
{
public function ClosureAS3()
{
init() //giv-em-the-jit :)
}

public function init():void
{
//create a counter
var counter1:Function = newCounter();
trace( counter1() ); //1
trace( counter1() ); //2
trace( counter1() ); //3
var counter2:Function = newCounter();
trace( counter2() ); //1
trace( counter2() ); //2
trace( counter1() ); //4 --> scope of i is still with counter1...cool! :)
}

public function newCounter():Function
{
var i:int = 0; //variable i gets bound into returned anonymous function via method Closure
return function():int
{
//i is available to the scope of the anonymous function
i=i+1;
return i;
}
}
}
}

Scary, functional AS3. :)

Cheers,

Ted :)

5 Responses to “Simple Method Closure in AS3”

  1. # Blogger johnny

    Scary good, or scary bad?  

  2. # Anonymous Dirk

    you're invoking the function at assignment time already which is not necessary (i.e. you do not need to add parenthesis when you assign the function reference)

    // just take the reference without invoking it
    var counter1:Function = newCounter;  

  3. # Blogger Marcus

    How do you remove the closure (and the variable it refers to) from memory?  

  4. # Blogger Ted Patrick

    Dirk,

    Everytime newCounter is called a new anonymous function is created and returned. As the function is returned the local variable i becomes bound creating the closure. Although i was a local variable it is now scoped within the new function and isn't local or global. If I returned a reference to newCounter directly, the closure will not get created.

    I checked with an engineer on player team who works on the VM in regards to closures and its effects on memory and reference. Provided that the object bound into the closure has no references to other objects, the closure and all variables will garbage collect when all references to the function are destroyed.  

  5. # Anonymous John

    Hi…

    I am really new to Flex, I mean I am just 2 weeks doing Flex. I had encountered some difficulties…

    I had a button and the click event will call a function that returns a value of type string.

    Below is the code;
    click=”editState()”

    This event cannot be raised as this function requires a string value to pass and executes the function.

    Below is the function code;
    private function editState(passedValue:String):void {
    //code goes here
    }

    Now, what I want to do is to capture a label.text value and put it in the call function under click event but how am I supposed to do that? I tried putting like editState(’label.text’). Sorry, I am very new… Please advice

    –John–  

Post a Comment

Where to find me:

Ted on Twitter - @__ted__
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe

Latest

Lists

Links

Jobs

city, state, zip

Archives