Twitter: raymondcamden


Address: Lafayette, LA, USA

This works... and it freaks me out

12-11-2013 3,730 views ColdFusion 3 Comments

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. ;)

3 Comments

  • Commented on 12-11-2013 at 11:30 PM
    It basically creates a closure. You can use namespacing like you would in javascript as well.
    https://gist.github.com/dfgrumpy/7923587
  • Commented on 12-12-2013 at 7:05 AM
    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?
  • Commented on 12-12-2013 at 9:51 AM
    It's just extra parentheses, it does not cause the function to use closure. Let me gist... https://gist.github.com/daccfml/7930169

    --
    Adam

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

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty