CoffeeScript
![]() |
|
Paradigm(s) | Multi-paradigm: prototype-based, functional, imperative, scripting |
---|---|
Appeared in | 2009 |
Designed by | Jeremy Ashkenas |
Developer | Jeremy Ashkenas, et al. |
Stable release | 1.3.3 (May 15, 2012 | )
Influenced by | Haskell,[1] JavaScript, Perl, Python,[1] Ruby,[1] YAML[2] |
Influenced | MoonScript |
OS | Cross-platform |
License | MIT License |
Usual filename extensions | .coffee |
Website | coffeescript.org |
CoffeeScript is a programming language that transcompiles to JavaScript. The language adds syntactic sugar inspired by Ruby, Python and Haskell[1] to enhance JavaScript's brevity and readability, and add more sophisticated features like list comprehension and pattern matching. CoffeeScript compiles predictably to JavaScript and programs can be written with less code, typically 1/3 fewer lines, with no effect on runtime performance.[3] Since March 16, 2011, CoffeeScript has been on GitHub's list of most-watched projects[4], and as of 29 August 2012 is the eleventh most popular language on GitHub[5].
The language has a relatively large following in the Ruby community. CoffeeScript support is included in Ruby on Rails version 3.1.[6] Additionally, Brendan Eich has referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.[7][8]
Contents |
History
On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment: "initial commit of the mystery language."[9] The compiler was written in Ruby. On December 24, he made the first tagged and documented release, 0.1.0. On February 21, 2010, he committed version 0.5, which replaced the Ruby compiler with one written in pure CoffeeScript. By that time the project had attracted several other contributors on GitHub, and was receiving over 300 page hits per day.
On December 24, 2010, Ashkenas announced the release of stable 1.0.0 to Hacker News, the site where the project was announced for the first time.[10][11]
Syntax
Many JavaScript statements are usable as expressions in CoffeeScript, for example if
, switch
and for
. These control statements also have postfix versions.
A general principle is that many unnecessary parentheses and braces can be dropped because indention can be used instead of braces to denote blocks of code, function calls are implicit (the parentheses needed for a function call in JavaScript can be dropped), object literals are often detected automatically and so on.
Examples
A common JavaScript snippet using the jQuery library is
$(document).ready(function() { // Initialization code goes here });
Or even just
$(function() { // Initialization code goes here });
In CoffeeScript, the function
keyword is replaced by the ->
symbol, and indentation is used instead of curly braces, as in other off-side rule languages such as Python and Haskell. Also, parentheses can usually be omitted. Thus, the CoffeeScript equivalent of the snippet above is
$(document).ready -> # Initialization code goes here
Or
($ document).ready -> # Initialization code goes here
Or just
$ -> # Initialization code goes here
Compiling
The CoffeeScript compiler has been written in CoffeeScript since version 0.5 and is available as a Node.js utility; however, the core compiler does not rely on Node.js and can be run in any JavaScript environment.[12] One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven build system. The plugin uses the Rhino JavaScript engine written in Java.
The official site at CoffeeScript.org has a "Try CoffeeScript" button in the menu bar; clicking it opens a modal window in which users can enter CoffeeScript, see the JavaScript output, and run it directly in the browser. The js2coffee[13] site provides bi-directional translation.
See also
References
- ^ a b c d The Changelog. Episode 0.2.9 - CoffeeScript with Jeremy Ashkenas, July 23, 2010
- ^ Heller, Martin (18 October 2011). "Turn up your nose at Dart and smell the CoffeeScript". JavaWorld (InfoWorld). http://www.javaworld.com/javaworld/jw-10-2011/111018-coffeescript-vs-dart.html. Retrieved 2012-02-09.
- ^ Read Write Hack. Interview with Jeremy Ashkenas, Jan 7, 2011
- ^ Github. Popular Watched Repositories
- ^ GitHub. CoffeeScript
- ^ Peek, Joshua. Tweet by Rails Core Team Member on Apr 13, 2011
- ^ Eich, Brendan. "Harmony of My Dreams"
- ^ Eich, Brendan. "My JSConf.US Presentation"
- ^ Github. 'initial commit of the mystery language'
- ^ Hacker News. CoffeeScript 1.0.0 announcement posted by Jeremy Ashkenas on Dec 24, 2010
- ^ Hacker News. Original CoffeeScript announcement posted by Jeremy Ashkenas on Dec 24, 2009
- ^ http://jashkenas.github.com/coffee-script/#installation
- ^ "jscoffee". http://js2coffee.org.
Further reading
- Grosenbach, Geoffrey (May 12, 2011). Meet CoffeeScript (First ed.). PeepCode. https://peepcode.com/products/coffeescript
- Bates, Mark (May 31, 2012). Programming in CoffeeScript (First ed.). Addison-Wesley. pp. 350. ISBN 0-321-82010-X. http://my.safaribooksonline.com/book/-/9780132946155.
- MacCaw, Alex (January 31, 2012). The Little Book on CoffeeScript (First ed.). O'Reilly Media. pp. 62. ISBN 978-1449321055. http://arcturo.github.com/library/coffeescript/.
- Burnham, Trevor (August 3, 2011). CoffeeScript: Accelerated JavaScript Development (First ed.). Pragmatic Bookshelf. pp. 138. ISBN 978-1934356784. http://pragprog.com/book/tbcoffee/coffeescript.
External links
- Official website
- CoffeeScript repository at GitHub