The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Actually understanding closures in ColdFusion 10

posted under category: ColdFusion on March 11, 2012 by Nathan

There has been a lot of confusion about closures in ColdFusion 10, and I don't think anyone has done a good enough job explaining it. I'm going to try to make it easy.

First, understand that what we call the closures feature is actually a handful of related ideas: anonymous functions, named function expressions, functions as first-class citizens, nested functions, and yes, true closures.

An anonymous function is simply a function without a name. You define it inline, usually as an argument to a function call or as a function's return value. Eventually it may have a name, but at the time and scope of its creation it does not.

A named function expression is another way to define a function. Instead of the classic function operator syntax, function name() {}, you define it a lot like any other variable. Name equals value. var name = function() {};.

Functions as first-class citizens finally reinforces something we have had unofficially since ColdFusion 6.1, passing a function as an argument. The difference now is there is a function data type. This helps us formalize a functional programming paradigm in a previously object-oriented CFML.

Having nested functions means that you can define a function within another. At its most basic, you can categorize you methods, hiding them in another function, and make little helpers that you only call from that defining parent function. You can define them in the classic function operator syntax function name() {} or in the expressed var name = function() {};.

Here is where it gets fun.

That inner function has access to the var scope of the function it was defined from. This is what a closure is. It knows about its origin and it doesn't forget. It will always be tied to that same parent var scope.

Combining those ideas, you can define a function within another and pass it out (with or without assigning a variable name to it), then that outbound function can use its closure abilities to reference data in the method it came from. This is where much of the power of Javascript comes from, and provides a good start to doing functional programming.

The similarities between what CFML will do in CF10 and what Javascript does are incredible. I love the direction we have seen so far, and I am excited for the future of ColdFusion!

Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.