TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Method Combinators in CoffeeScript

64 点作者 grifaton超过 12 年前

4 条评论

asolove超过 12 年前
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 未加载
goblin89超过 12 年前
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 未加载
raganwald超过 12 年前
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 未加载
quarterto超过 12 年前
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 未加载