Content deleted Content added
Jerryobject (talk | contribs) m In prior edit and this edit: nonacronym MOS:ALLCAPS, nonlead-word nonproper noun MOS:CAPS > WP:LOWERCASE sentence case. WP:LINKs: update-standardize, add. Small WP:COPYEDIT WP:EoS WP:TERSE: cut needless word repeat. Temporary diff alignment placeholder carriage return cut. |
|||
(28 intermediate revisions by 27 users not shown) | |||
Line 1:
{{Short description|Programming language which compiles to JavaScript}}
{{Infobox programming language
| name = CoffeeScript
| logo = CoffeeScript-logo.
|
| family = [[ECMAScript]]
| released = {{start date and age|2009|12|13}}▼
| designer = [[Jeremy Ashkenas]]
| developer =
|
| latest release date = {{start date and age|{{Wikidata|qualifier| Q1106819 |P348|P577}}}}
| typing = [[Dynamic typing|dynamic]], [[Latent typing|implicit]]
| influenced_by = [[Haskell (programming language)|Haskell]], [[JavaScript]], [[Perl]],{{citation needed|date=January 2016}} [[Python (programming language)|Python]],<ref>https://coffeescript.org/ "CoffeeScript borrows chained comparisons from Python"</ref> [[Ruby (programming language)|Ruby]], [[YAML]]<ref name="smell">{{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}}</ref>▼
| scope = [[Scope (computer science)|lexical]]
| influenced = MoonScript, [[LiveScript]], JavaScript▼
| programming language = CoffeeScript
|
| operating system = [[Cross-platform software|Cross-platform]]
| file_ext = .coffee, .litcoffee{{citation needed|date=September 2020}}▼
| license = [[MIT License|MIT]]
▲|
▲|
▲| influenced = [[MoonScript]], [[LiveScript (programming language)|LiveScript]], JavaScript
}}
'''CoffeeScript''' is a [[programming language]] that compiles to [[JavaScript]]. It adds [[syntactic sugar]] inspired by [[Ruby (programming language)|Ruby]], [[Python (programming language)|Python]], and [[
CoffeeScript support is included in [[Ruby on Rails]] version 3.1<ref>{{cite web |
== History ==
On December 13, 2009, [[Jeremy Ashkenas]] made the first [[Git (software)|Git]] commit of CoffeeScript with the comment
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.<ref>Hacker News. [https://news.ycombinator.com/item?id=2037801 CoffeeScript 1.0.0 announcement] posted by Jeremy Ashkenas on Dec 24, 2010</ref><ref>Hacker News. [https://news.ycombinator.com/item?id=1014080 Original CoffeeScript announcement] posted by Jeremy Ashkenas on Dec 24, 2009</ref>
On September 18, 2017, version 2.0.0 was introduced,<ref>coffeescript.org [http://coffeescript.org/announcing-coffeescript-2/ Announcing CoffeeScript 2]</ref> which "aims to bring CoffeeScript into the modern JavaScript era, closing gaps in compatibility with JavaScript while preserving the clean syntax that is
== Syntax ==
Almost everything is an [[expression (computer science)|expression]] in CoffeeScript, for example, <code>if</code>, <code>switch</code> and <code>for</code> 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, <code>if</code> can also be written in <code> consequent if condition</code> 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]]
<syntaxhighlight lang="javascript">
if (18.5 <= BMI && BMI < 25)
</syntaxhighlight>
Line 50 ⟶ 55:
</syntaxhighlight>
To compute the [[greatest common divisor]] of two integers with the [[
<syntaxhighlight lang="javascript">let gcd = (x, y) => {
do {
[x, y] = [y, x%y];
} while (y !== 0)
return x;
}</syntaxhighlight>▼
▲</syntaxhighlight>
Whereas in CoffeeScript one can use <code>until</code>
<syntaxhighlight lang="coffeescript">
gcd = (x, y) ->
Line 67 ⟶ 70:
x
</syntaxhighlight>
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:▼
<syntaxhighlight lang="coffeescript">▼
alert n*n for n in [1..10] when n%2 is 1▼
</syntaxhighlight>▼
Alternatively, there is:▼
<syntaxhighlight lang="coffeescript">▼
alert n*n for n in [1..10] by 2▼
</syntaxhighlight>▼
A [[linear search]] can be implemented with a one-liner using the when keyword:▼
<syntaxhighlight lang="coffeescript">▼
names = ["Ivan", "Joanna", "Nikolay", "Mihaela"]▼
linearSearch = (searchName) -> alert(name) for name in names when name is searchName▼
</syntaxhighlight>▼
The <code>for ... in</code> syntax allows looping over arrays while the <code>for ... of</code> syntax allows looping over objects.▼
The <code>?</code> keyword quickly checks if a variable is <code>null</code> or <code>undefined</code> :
Line 101 ⟶ 83:
This would alert "No person" if the variable is <code>null</code> or <code>undefined</code> and "Have person" if there is something there.
A common pre-[[ES6]] JavaScript snippet using the [[jQuery]] library is:
<syntaxhighlight lang="javascript">
$(document).ready(function() {
// Initialization code goes here
});
</syntaxhighlight>
Line 114 ⟶ 96:
$(function() {
// Initialization code goes here
});
</syntaxhighlight>
In CoffeeScript, the <code>function</code> keyword is replaced by the <code>-></code> symbol, and indentation is used instead of curly braces, as in other [[off-side rule]] languages such as Python and Haskell.
<!-- Ruby is probably the most similar language that GeSHi supports -->
Line 141 ⟶ 123:
</syntaxhighlight>
▲Any [[For loop|''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:
CoffeeScript has been criticized for its unusual scoping▼
▲<syntaxhighlight lang="coffeescript">
▲alert n*n for n in [1..10] when n%2 is 1
▲</syntaxhighlight>
▲Alternatively, there is:
▲<syntaxhighlight lang="coffeescript">
▲alert n*n for n in [1..10] by 2
▲</syntaxhighlight>
▲A [[linear search]] can be implemented with a one-liner using the when keyword:
▲<syntaxhighlight lang="coffeescript">
▲names = ["Ivan", "Joanna", "Nikolay", "Mihaela"]
▲linearSearch = (searchName) -> alert(name) for name in names when name is searchName
▲</syntaxhighlight>
▲The <code>for ... in</code> syntax allows looping over arrays while the <code>for ... of</code> syntax allows looping over objects.
▲CoffeeScript has been criticized for its unusual [[Scope (computer science)|scoping]] rules.<ref>{{cite web
|url=http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/
|title=The Problem with Implicit Scoping in CoffeeScript
Line 149 ⟶ 151:
|url=https://donatstudios.com/CoffeeScript-Madness
|title=CoffeeScript's Scoping is Madness
|date=25 July 2013
|access-date=2018-10-13
}}</ref> 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.▼
▲by and taken for granted since [[procedural programming]]
For example, with the following code snippet in JavaScript
Line 163 ⟶ 164:
// ...
function baz() {
var foo = "bar";
console.log(`foo = ${foo}`);
}
// ...
Line 170 ⟶ 171:
</syntaxhighlight>
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 (compilers)|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.<ref>[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.</ref> 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)|Rhino]] JavaScript engine written in [[Java (programming language)|Java]].{{citation needed|date=May 2019}}
Line 180 ⟶ 179:
== 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 [[
== Extensions ==
Line 187:
== Adoption ==
On September 13, 2012, [[Dropbox (service)|Dropbox]] announced that their browser-side code base had been rewritten from [[JavaScript]] to CoffeeScript,<ref>{{cite web|url=https://
[[GitHub]]'s internal style guide once said "write new JS in CoffeeScript",
[[Pixel Game Maker MV]] makes uses of CoffeeScript as part of its game development environment.<ref name="CCG">{{cite web |last1=Cullen |first1=Daniel |title=
== See also ==
Line 202:
* [[Dart (programming language)]]
* [[Kotlin (programming language)]]
* [[LiveScript (programming language)]]
* [[Opa (programming language)]]
* [[Elm (programming language)]]
Line 209:
== References ==
{{Reflist
== Further reading ==
* {{Cite book |last1=Lee |first1=Patrick |date=May 14, 2014 |title=CoffeeScript in Action |edition=
* {{Cite
* {{Cite book |last1=Bates |first1=Mark |date=May 31, 2012 |title=Programming in CoffeeScript |edition=
* {{Cite book |last1=
== External links ==
* {{Official website}}
{{Programming languages}}▼
{{JavaScript
{{NodeJs}}
▲{{Programming languages}}
{{Authority control}}
Line 234:
[[Category:2009 software]]
[[Category:Free software projects]]
<!-- Hidden categories below -->
[[Category:Articles with example JavaScript code]]
|