Pular para o conteúdo

Conheça Walt Disney World

CoffeeScript

CoffeeScript
CoffeeScript-logo.png
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; 3 months ago (2012-05-15))
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

  1. ^ a b c d The Changelog. Episode 0.2.9 - CoffeeScript with Jeremy Ashkenas, July 23, 2010
  2. ^ 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. 
  3. ^ Read Write Hack. Interview with Jeremy Ashkenas, Jan 7, 2011
  4. ^ Github. Popular Watched Repositories
  5. ^ GitHub. CoffeeScript
  6. ^ Peek, Joshua. Tweet by Rails Core Team Member on Apr 13, 2011
  7. ^ Eich, Brendan. "Harmony of My Dreams"
  8. ^ Eich, Brendan. "My JSConf.US Presentation"
  9. ^ Github. 'initial commit of the mystery language'
  10. ^ Hacker News. CoffeeScript 1.0.0 announcement posted by Jeremy Ashkenas on Dec 24, 2010
  11. ^ Hacker News. Original CoffeeScript announcement posted by Jeremy Ashkenas on Dec 24, 2009
  12. ^ http://jashkenas.github.com/coffee-script/#installation
  13. ^ "jscoffee". http://js2coffee.org. 

Further reading

External links

Personal tools
  • Create account
  • Log in
Namespaces

Variants
Actions
Navigation
Toolbox
Print/export