TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Method Combinators in CoffeeScript

64 pointsby grifatonover 12 years ago

4 comments

asoloveover 12 years ago
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.
评论 #4439446 未加载
goblin89over 12 years ago
A little nitpick, which may be a misconception deserving clarification:<p>&#62; 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.
评论 #4440569 未加载
raganwaldover 12 years ago
Jeremy Ashkenas provides the "tl;dr:"<p><i>Python decorators are a hack around the lack of proper lambda ;) Just pass the function: decorate -&#62; … Where "decorate" is a fn</i><p><a href="https://twitter.com/jashkenas/status/235012485009248256" rel="nofollow">https://twitter.com/jashkenas/status/235012485009248256</a>
评论 #4440451 未加载
quartertoover 12 years ago
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>
评论 #4452267 未加载