Jump to content

CoffeeScript

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 180.252.99.125 (talk) at 02:33, 1 April 2012 (History: Remove advertisement). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
CoffeeScript
File:CoffeeScript-logo.png
ParadigmMulti-paradigm: prototype-based, functional, imperative, scripting
Designed byJeremy Ashkenas
DeveloperJeremy Ashkenas, et al.
First appeared2009
Stable release
1.2.0 / December 18, 2011 (2011-12-18)
OSCross-platform
LicenseMIT License
Filename extensions.coffee
Websitecoffeescript.org
Influenced by
JavaScript, Python,[1] Ruby,[1] Haskell,[1] YAML[2]
Influenced
MoonScript

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, as well as adding more sophisticated features like array 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]

The language has a relatively large following in the Ruby community. CoffeeScript support is included in Ruby on Rails version 3.1.[5] Additionally, Brendan Eich has referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.[6][7]

History

On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment: "initial commit of the mystery language."[8] 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.[9][10]

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 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 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[11]. One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven build system. The plugin uses Mozilla Rhino, a 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 you can enter CoffeeScript, see the JavaScript output, and run it directly in the browser. The jscoffee site provides bi-directional translation.

References

  1. ^ a b c d The Changelog. Episode 0.2.9 - CoffeeScript with Jeremy Ashkenas, Jul 23, 2010
  2. ^ Heller, Martin (18 October 2011). "Turn up your nose at Dart and smell the CoffeeScript". JavaWorld. InfoWorld. Retrieved 2012-02-09.
  3. ^ Read Write Hack. Interview with Jeremy Ashkenas, Jan 7, 2011
  4. ^ Github. Popular Watched Repositories
  5. ^ Peek, Joshua. Tweet by Rails Core Team Member on Apr 13, 2011
  6. ^ Eich, Brendan. "Harmony of My Dreams"
  7. ^ Eich, Brendan. "My JSConf.US Presentation"
  8. ^ Github. 'initial commit of the mystery language'
  9. ^ Hacker News. CoffeeScript 1.0.0 announcement posted by Jeremy Ashkenas on Dec 24, 2010
  10. ^ Hacker News. Original CoffeeScript announcement posted by Jeremy Ashkenas on Dec 24, 2009
  11. ^ http://jashkenas.github.com/coffee-script/#installation

Further reading

Tutorials