CoffeeScript
Encyclopedia
CoffeeScript is a programming language that transcompiles
Transcompiler
A transcompiler is a special compiler that translates the source code of a programming language into the source code of another programming language, e.g...

 to JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

. The language adds syntactic sugar
Syntactic sugar
Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express....

 inspired by Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 and Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

  to enhance JavaScript's brevity and readability, as well as adding more sophisticated features like array comprehension and pattern matching
Pattern matching
In computer science, pattern matching is the act of checking some sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact. The patterns generally have the form of either sequences or tree structures...

. CoffeeScript compiles predictably to JavaScript and programs can be written with less code (typically 1/3 fewer lines) with no effect on runtime performance. Since March 16, 2011, CoffeeScript has been on GitHub
Github
GitHub is a web-based hosting service for software development projects that use the Git revision control system. GitHub offers both commercial plans and free accounts for open source projects...

's list of most-watched projects.

The language has a relatively large following in the Ruby community, and has been used in production by 37signals
37signals
37signals is a privately held web application company based in Chicago, Illinois. The firm was co-founded in 1999 by Jason Fried, Carlos Segura, and Ernest Kim as a web design company. Segura left in 2000 and Kim left in 2003, leaving Fried as the only remaining founder.Since mid-2004, the...

. CoffeeScript support is included in Ruby on Rails
Ruby on Rails
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language.-History:...

 version 3.1. Additionally, Brendan Eich
Brendan Eich
Brendan Eich is a computer programmer and creator of the JavaScript scripting language. He is the chief technology officer at the Mozilla Corporation.-Education:...

 has referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.

History

On December 13, 2009, Jeremy Ashkenas made the first Git
Git (software)
Git is a distributed revision control system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development. Every Git working directory is a full-fledged repository with complete history and full revision tracking capabilities, not dependent on...

 commit of CoffeeScript with the comment: "initial commit of the mystery language." CoffeeScript was created after reading the "Create Your Own Programming Language" ebook. 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
Github
GitHub is a web-based hosting service for software development projects that use the Git revision control system. GitHub offers both commercial plans and free accounts for open source projects...

, 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
Hacker News
Hacker News is a social news website about computer hacking and startup companies, run by Paul Graham's funding firm Y Combinator. It is different from other social news websites in that there is no option to down vote submissions; submissions can either be voted up or not voted on at all...

, the site where the project was announced for the first time.

Examples

A common JavaScript snippet using the jQuery
JQuery
jQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML. It was released in January 2006 at BarCamp NYC by John Resig...

 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
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 and Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

. 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
Self-hosting
The term self-hosting was coined to refer to the use of a computer program as part of the toolchain or operating system that produces new versions of that same program—for example, a compiler that can compile its own source code. Self-hosting software is commonplace on personal computers and larger...

 since version 0.5 and is available as a Node.js
Node.js
Node.js is a software system designed for writing highly-scalable internet applications, notably web servers.Programs are written in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability....

 utility; however, the core compiler does not rely on Node.js
Node.js
Node.js is a software system designed for writing highly-scalable internet applications, notably web servers.Programs are written in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability....

 and can be run in any JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 environment. One alternative to the Node.js
Node.js
Node.js is a software system designed for writing highly-scalable internet applications, notably web servers.Programs are written in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability....

 utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven
Apache Maven
Maven is a build automation and software comprehension tool. While primarily used for Java programming, it can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. Maven serves a similar purpose to the Apache Ant tool, but it is based on different concepts and...

 build system. The plugin works by utilizing Mozilla Rhino, a JavaScript engine written in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

. 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.

External links


Tutorials
  • CoffeeScript Cookbook, collection of community sourced CoffeeScript recipes Repository
  • The Little Book on CoffeeScript, book introducing CoffeeScript and contrasting it with JavaScript Repository
  • Smooth CoffeeScript, book on CoffeeScript and functional programming
    Functional programming
    In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

    , also available as an online book
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK