CoffeeScript

{{Short description|Programming language which compiles to JavaScript}}

{{Infobox programming language

| name = CoffeeScript

| logo = CoffeeScript-logo.svg

| paradigms = Multi-paradigm: prototype-based, functional, imperative, scripting

| family = ECMAScript

| designer = Jeremy Ashkenas

| developer = same

| released = {{Start date and age|2009|12|13}}

| latest release version = {{Wikidata|property|reference|edit| Q1106819 |P348}}

| latest release date = {{start date and age|{{Wikidata|qualifier| Q1106819 |P348|P577}}}}

| typing = dynamic, implicit

| scope = lexical

| programming language = CoffeeScript

| platform = x86-64

| operating system = Cross-platform

| license = MIT

| file ext = .coffee, .litcoffee{{citation needed|date=September 2020}}

| influenced by = Haskell, JavaScript, Perl,{{citation needed|date=January 2016}} Python,https://coffeescript.org/ "CoffeeScript borrows chained comparisons from Python" Ruby, YAML{{cite news |last1=Heller |first1=Martin |date=2011-10-18 |df=mdy |url=https://www.infoworld.com/article/2078452/turn-up-your-nose-at-dart-and-smell-the-coffeescript.html |title=Turn up your nose at Dart and smell the CoffeeScript |work=InfoWorld |access-date=2020-07-15}}

| influenced = MoonScript, LiveScript, JavaScript

}}

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability.{{cite book |last1=MacCaw |first1=Alex |date=28 February 2012 |title=The Little Book on CoffeeScript: The JavaScript Developer's Guide to Building Better Web Apps |edition=1st |publisher=O'Reilly Media |language=en |isbn=978-1-4493-2105-5}}{{cite book |last1=Thomas |first1=Lucas |date=2017 |title=The Little Book on CoffeeScript |edition=1st |publisher=CreateSpace Independent Publishing Platform |language=en |isbn=978-1-5472-1072-5}} Some added features include list comprehension and destructuring assignment.

CoffeeScript support is included in Ruby on Rails version 3.1{{cite web |last1=Peek |first1=Josh |url=https://twitter.com/joshpeek/status/58184348742074368 |title=Tweet by Rails Core Team Member |date= April 13, 2011}} and Play Framework.{{Cite web |url=https://www.playframework.com/documentation/2.5.x/AssetsCoffeeScript |title=AssetsCoffeeScript - 2.5.x |website=www.playframework.com |access-date=2016-10-31}} In 2011, Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.Eich, Brendan. "[http://brendaneich.com/2011/01/harmony-of-my-dreams/ Harmony of My Dreams]"Eich, Brendan. "[http://brendaneich.com/2011/05/my-jsconf-us-presentation/ My JSConf.US Presentation]"

History

On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment "initial commit of the mystery language".Github. [https://github.com/jashkenas/coffee-script/commit/8e9d637985d2dc9b44922076ad54ffef7fa8e9c2 'initial commit of the mystery language'] 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 a self-hosting version 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.Hacker News. [https://news.ycombinator.com/item?id=2037801 CoffeeScript 1.0.0 announcement] posted by Jeremy Ashkenas on Dec 24, 2010Hacker News. [https://news.ycombinator.com/item?id=1014080 Original CoffeeScript announcement] posted by Jeremy Ashkenas on Dec 24, 2009

On September 18, 2017, version 2.0.0 was introduced,coffeescript.org [http://coffeescript.org/announcing-coffeescript-2/ Announcing CoffeeScript 2] which "aims to bring CoffeeScript into the modern JavaScript era, closing gaps in compatibility with JavaScript while preserving the clean syntax that is CoffeeScript's hallmark".

Syntax

Almost everything is an expression in CoffeeScript, for example, if, switch and for expressions (which have no return value in JavaScript) return a value. As in Perl and Ruby, these control statements also have postfix versions; for example, if can also be written in consequent if condition form.

Many unnecessary parentheses and braces can be omitted; for example, blocks of code can be denoted by indentation instead of braces, function calls are implicit, and object literals are often detected automatically.

To compute the body mass index in JavaScript, one could write:

let mass = 72;

let height = 1.78;

let BMI = mass / height**2;

if (18.5 <= BMI && BMI < 25) alert('You are healthy!');

With CoffeeScript the interval is directly described:

mass = 72

height = 1.78

BMI = mass / height**2

alert 'You are healthy!' if 18.5 <= BMI < 25

To compute the greatest common divisor of two integers with the Euclidean algorithm, in JavaScript one usually needs a while loop:

let gcd = (x, y) => {

do {

[x, y] = [y, x%y];

} while (y !== 0)

return x;

}

Whereas in CoffeeScript one can use untilCoffeeScript calls this "pattern matching", which is a non-standard use of that term. instead:

gcd = (x, y) ->

[x, y] = [y, x%y] until y is 0

x

The ? keyword quickly checks if a variable is null or undefined :

personCheck = ->

if not person? then alert("No person") else alert("Have person")

person = null

personCheck()

person = "Ivan"

personCheck()

This would alert "No person" if the variable is null or undefined and "Have person" if there is something there.

A common pre-ES6 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, using indentation level instead to denote a function or block. Thus, the CoffeeScript equivalent of the snippet above is:

$(document).ready ->

# Initialization code goes here

Or just:

$ ->

# Initialization code goes here

Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #{ ... }, and single-quoted strings are literal.{{cite web|title=Official CoffeeScript Page|url=http://coffeescript.org|access-date=20 November 2013}}

author = "Wittgenstein"

quote = "A picture is a fact. -- #{ author }"

sentence = "#{ 22 / 7 } is a decent approximation of π"

Any for loop can be replaced by a list comprehension; so that to compute the squares of the positive odd numbers smaller than ten (i.e. numbers whose remainder modulo 2 is 1), one can do:

alert n*n for n in [1..10] when n%2 is 1

Alternatively, there is:

alert n*n for n in [1..10] by 2

A linear search can be implemented with a one-liner using the when keyword:

names = ["Ivan", "Joanna", "Nikolay", "Mihaela"]

linearSearch = (searchName) -> alert(name) for name in names when name is searchName

The for ... in syntax allows looping over arrays while the for ... of syntax allows looping over objects.

CoffeeScript has been criticized for its unusual scoping rules.{{cite web

|url=http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/

|title=The Problem with Implicit Scoping in CoffeeScript

|access-date=2018-10-13

}}{{cite web

|url=https://donatstudios.com/CoffeeScript-Madness

|title=CoffeeScript's Scoping is Madness

|date=25 July 2013

|access-date=2018-10-13

}} In particular, it completely disallows variable shadowing which makes reasoning about code more difficult and error-prone in some basic programming patterns established

by and taken for granted since procedural programming principles were defined.

For example, with the following code snippet in JavaScript

one does not have to look outside the {}-block to know for

sure that no possible foo variable in the outer scope can be

incidentally overridden:

// ...

function baz() {

var foo = "bar";

console.log(`foo = ${foo}`);

}

// ...

}

In CoffeeScript there is no way to tell if the scope of a variable is limited to a block or not without looking outside the block.

Development and distribution

The CoffeeScript compiler has been self-hosting 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.[https://jashkenas.github.com/coffee-script/#installation CoffeeScript] {{webarchive|url=https://web.archive.org/web/20120427060308/http://jashkenas.github.com/coffee-script/ |date=2012-04-27 }}. Jashkenas.github.com. Retrieved on 2013-07-21. One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the Apache Maven build system. The plugin uses the Rhino JavaScript engine written in Java.{{citation needed|date=May 2019}}

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{{cite web |url=http://js2.coffee |title=js2coffee|last=Sta Cruz|first= Rico|access-date=11 May 2014}} site provides bi-directional translation.

Latest additions

{{uncited section|date=April 2024}}

  • Source maps allow users to debug their CoffeeScript code directly, supporting CoffeeScript tracebacks on run time errors.
  • CoffeeScript supports a form of literate programming, using the .coffee.md or .litcoffee file extension. This allows the source code to be written in Markdown. The compiler will treat any indented blocks (Markdown's way of indicating source code) as code, and ignore the rest as comments.

Extensions

Iced CoffeeScript is a superset of CoffeeScript which adds two new keywords: await and defer. These additions simplify asynchronous control flow, making the code look more like a procedural programming language, eliminating the call-back chain. It can be used on the server side and in the browser.{{cite web|url=http://maxtaco.github.io/coffee-script/ |title=Official IcedCoffeeScript website }}

Adoption

On September 13, 2012, Dropbox announced that their browser-side code base had been rewritten from JavaScript to CoffeeScript,{{cite web|url=https://dropbox.tech/application/dropbox-dives-into-coffeescript| title=Dropbox dives into CoffeeScript| date=13 September 2012|last1= Wheeler| first1=Dan| last2= Mahkovec|first2= Ziga |last3= Varenhorst |first3=Chris|access-date=11 May 2013}} however it was migrated to TypeScript in 2017.{{cite web |last1=Goldstein |first1=David |title=The Great CoffeeScript to Typescript Migration of 2017 |url=https://dropbox.tech/frontend/the-great-coffeescript-to-typescript-migration-of-2017 |website=Dropbox.Tech |access-date=30 June 2020 |date=13 May 2020}}

GitHub's internal style guide once said "write new JS in CoffeeScript", though it no longer does,{{cite web|url=https://github.com/styleguide/javascript|title=JavaScript · Styleguide · GitHub|publisher=Github.com|access-date=2015-11-30|archive-url=https://web.archive.org/web/20130815075924/https://github.com/styleguide/javascript|archive-date=2013-08-15|url-status=dead}} and its Atom text editor was also written in the language, with configuration written in CSON ("CoffeeScript Object Notation"), a variant of JSON.{{cite web |url=https://github.com/atom/atom |title=Atom source code |publisher=GitHub |access-date=2021-06-26}}{{cite web |title=Basic Customization |url=https://flight-manual.atom-editor.cc/using-atom/sections/basic-customization/#configuring-with-cson |website=Atom Flight Manual |publisher=GitHub |access-date=29 April 2024 |language=en |url-status=live |archive-url=https://web.archive.org/web/20240429181349/https://flight-manual.atom-editor.cc/using-atom/sections/basic-customization/ |archive-date=2024-04-29}}

Pixel Game Maker MV makes uses of CoffeeScript as part of its game development environment.{{cite web |last1=Cullen |first1=Daniel |title=Pixel Game Maker MV (PC) |url=https://www.christcenteredgamer.com/reviews/pc-mac/pixel-game-maker-mv-pc |website=Christ Centered Gaming |access-date=15 January 2021}}

See also

References

{{Reflist}}

Further reading

  • {{Cite book |last1=Lee |first1=Patrick |date=May 14, 2014 |title=CoffeeScript in Action |edition=1st |publisher=Manning Publications |page=432 |isbn=978-1617290626}}
  • {{Cite book |last1=Grosenbach |first1=Geoffrey |date=May 12, 2011 |title=Meet CoffeeScript |edition=1st |publisher=PeepCode}}
  • {{Cite book |last1=Bates |first1=Mark |date=May 31, 2012 |title=Programming in CoffeeScript |edition=1st |publisher=Addison-Wesley |page=350 |isbn=978-0-321-82010-5}}
  • {{Cite book |last1=Burnham |first1=Trevor |date=August 3, 2011 |title=CoffeeScript: Accelerated JavaScript Development |edition=1st |publisher=Pragmatic Bookshelf |page=[https://archive.org/details/isbn_9781934356784/page/138 138] |isbn=978-1934356784 |url-access=registration |url=https://archive.org/details/isbn_9781934356784/page/138}}