Immediately-Invoked Function Expressions are an easy way to hide your variables, preventing collisions with your code or the code of others. In JavaScript you'll see the following a lot:
In CoffeeScript the syntax can be the same; enclose the function in parens, and put parens after it to call it. It's ugly, though:
CoffeeScript has the do keyword, though, and it provides a cleaner way to do this:
If you're passing an argument the ugly way you'd put the closing paren at the same level as the IIFE's code block; this looks a little wonky to me, and the trailing paren looks lonely (odd for a Lisp person to say, huh?
The do version requires the variable to be set at the point of the do. I think I still prefer this method, but it reads a little funny to me:
No comments:
Post a Comment