guilin 桂林
guilin 桂林

Reputation: 17422

why as3 coder does not use closure?

I see many code like this

foo.addListener(Event.CLICK, clickHandler);

function clickHandler(event:Event):void {
   //...
}

but, why not?

foo.addListener(Event.CLICK, function(event:Event):void {
  //...
});

I think the below is better, I am using flex sdk 4.5.1, I don't know if it related with the version, or something about optimizations?

Upvotes: 1

Views: 164

Answers (2)

merv
merv

Reputation: 77098

Performance is not a minor consideration: http://gskinner.com/talks/quick/#52

Upvotes: 1

zzzzBov
zzzzBov

Reputation: 179256

Even in other ECMAScript variants (ok, so JavaScript) it is typically better to bind an event using a reference to a function than to dump in an anonymous function.

The reason it's better to use the first form is when it comes to unbinding the event. If you have a reference to the bound event, you can unbind it, otherwise you're stuck having to unbind the event other ways.


Other information, all functions in ECMAScript are closures. They close over the contained variables and set a new scope. Both examples use closures.

Upvotes: 5

Related Questions