This works... and it freaks me out

This post is more than 2 years old.

I just ran across a template that had an interesting typo. Here is an example.

component {
{
	public function hello() {
		return "Hello";
	}

}

}

See it? The function is surrounded by an extra pair of brackets. ColdFusion just hoists it up just like JavaScript, so I shouldn't be surprised, but wow did that freak me out a bit.

Then for the hell of it I went crazy - and yes - it still works.

component {

	{
		{
			{
				{
					{
						public function hello() {
							return "Hello";
						}
					}
				}
			}
		}
	}

}

Anyway... yeah... don't do this. ;)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Dave Ferguson posted on 12/12/2013 at 10:30 AM

It basically creates a closure. You can use namespacing like you would in javascript as well.
https://gist.github.com/dfg...

Comment 2 by Raymond Camden posted on 12/12/2013 at 6:05 PM

I'm not sure I agree with you. By itself the extra {} aren't making a closure - not in the example above. In yours you are when you *specifically* return the function, but by *itself*, as in the code above caused by a mistake on the dev's part, it isn't a closure.

Right?

Comment 3 by Adam Cameron posted on 12/12/2013 at 8:51 PM

It's just extra parentheses, it does not cause the function to use closure. Let me gist... https://gist.github.com/dac...

--
Adam

(was tempted to post all the code here, as I know how much you like that, Ray ;-)