While the syntax here is CoffeeScript-specific, this is a perfectly fine pattern to apply in any JavaScript.<p>The key is to remember that, in JavaScript, you aren't really "defining instance methods" in the way you are in almost any other language. You're just passing anonymous functions as the value tied to some key in a dictionary, which happens to be the prototype of some set of objects.
Once you get that into your mind and let it play around for a bit, you realize that you could be getting the anonymous function from anywhere!<p>From a function-factory, that takes some arguments and returns a function:<p><pre><code> render: renderWithTemplate("some_template")
</code></pre>
From an anonymous function wrapped in something that controls its execution:<p><pre><code> onScroll: _.debounce(100, function(){ /* do something */ })
</code></pre>
Or even by delegating to another object:<p><pre><code> viewInstance.onScroll = _.bind(somethingElse.render, somethingElse);
</code></pre>
Thanks for the great post to help bend our minds around these possibilities.
A little nitpick, which may be a misconception deserving clarification:<p>> Our decorators work just like Python method decorators, only we don't need any magic syntax for them because CoffeeScript, like JavaScript, already has this idea that functions can return functions and there's nothing particularly magic about defining a method, it's just an expression that evaluates to a function.<p>Python also has this idea (called ‘higher-order functions’). The difference is in syntax—function calls require parens in Python, and anonymous functions aren't that well supported. Therefore the need for special syntax construct to make decorator use convenient.
Jeremy Ashkenas provides the "tl;dr:"<p><i>Python decorators are a hack around the lack of proper lambda ;) Just pass the function: decorate -> … Where "decorate" is a fn</i><p><a href="https://twitter.com/jashkenas/status/235012485009248256" rel="nofollow">https://twitter.com/jashkenas/status/235012485009248256</a>
In LiveScript: <a href="https://github.com/quarterto/homoiconic/blob/d6b78812c00eb4df310a9b2067a8a89519419be9/2012/08/method-decorators-and-combinators-in-coffeescript.md" rel="nofollow">https://github.com/quarterto/homoiconic/blob/d6b78812c00eb4d...</a>