Barack Obama - Change We Can Believe In

Viewing By Entry / Main
July 4, 2008
Most of my readers will know that I'm a big fan of closures - the ability to create anonymous functions with bound variables (bound in the context of the closure).

David Harris has a brief post about the joys of jQuery where he comments on what, to him, was an unexpected feature that allowed binding of variables, i.e., JavaScript (and ActionScript) support closures. I find it interesting that he thinks this binding "somehow feels wrong" and I've seen the same reaction from a number of people when first confronted with closures.

This binding is what gives closures their power and why they are more than just "anonymous functions". They carry with them the context in which they were created.

It's also an interesting contrast with ColdFusion where variable binding is done at the last minute at runtime and thus you can take a UDF function that references variables.foo (which might be a page scope variable) and add that function to a CFC instance and now variables.foo will refer to foo in the variables scope of the CFC itself.

I think that's why my Closures for CFMX library has proved so puzzling for a lot of folks :)

Comments

I think this would be a great topic to put online. In fact it seems to me that it would be cool if we could take topics like this and YouTube them... 10 minute tutorials. (And create a site for CF and related topics to CFers. CFMLSchools.com or something like that!)


about closures.....

I'm conserned that they might pose a memory leak when used often. SInce there is no reference to it, how can it be garbage collected. I can add an event listener for the closure's parent function, but I can't unregister that listener.

Any ideas?


@Alan, not sure what you mean about "no reference to it". I don't see why you think it would cause a problem for GC. Could you elaborate?


When I played with them myself, I was unable to unregister the event listener, and it concerned me.

Perhaps it's not an issue for a function rarely called, but I was using it with an enterFrame event and I was unable to stop the event from being called by using a removeEventLListener(Event.ENTER_FRAME, (my function with closure).

Because I was only refering to the enter frame function Indirectly ( only through a parent function) it seemed odd.

Do am I incorrect anywhere?


@Alan, sorry but I don't understand what you are concerned about. Are you actually talking about jQuery? (I don't think so).


Don't take me as a perfect stupid, but what these "closures" are usuful for ? Could you make me a couple of examples ?

Thanks

Sergio


@Sergio, sure - have a read through my previous blog postings on closures:

http://corfield.org/search/closures


Alan is talking AS3 from what I can see, and I think he is right.

I have a friend who is much more clued up on AS3 who gave me some feed back telling me not to use anon functions in AS3.

I'll check up why again and do a follow up blog post, so keep an eye on my blog :-)


@David, thanx for the clarification. What's your blog?


Post Your Comments
Name:
Email Address:
Comments
*** Please note that all comments require moderation so it may be some time before your comment posts to this blog! ***
Remember My Information:
 



Hosting provided by