diff --git a/chapters/functions/parentheses.textile b/chapters/functions/parentheses.textile index 4f167f6..64f3cc4 100644 --- a/chapters/functions/parentheses.textile +++ b/chapters/functions/parentheses.textile @@ -12,10 +12,28 @@ h2. Solution Use parentheses anyway. +Another alternative is to utilise the do-notation like so: +{% highlight coffeescript %} +notify = -> alert "Hello, user!" +do notify if condition +{% endhighlight %} + +This compiles to the following JavaScript: +{% highlight javascript %} +var notify; +notify = function() { + return alert("Hello, user!"); +}; +if (condition) { + notify(); +} +{% endhiglight %} + h2. Discussion Like Ruby, CoffeeScript allows you to drop parentheses to method calls. Unlike Ruby, however, CoffeeScript treats a bare function name as the pointer to the function. The practical upshot of this is that if you give no arguments to a method, CoffeeScript cannot tell if you want to call the function or use it as a reference. Is this good or bad? It's just different. It creates an unexpected syntax case—parentheses aren't _always_ optional—but in exchange it gives you the ability to pass and receive functions fluently by name, something that's a bit klunky in Ruby. - +This usage of the do-notation is a neat approach for CoffeeScript with parenphobia. +Some people simply prefer to write out the parentheses in the function call, though. \ No newline at end of file