CoffeeScript: Difference between revisions

Content deleted Content added
link fix
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.
 
(12 intermediate revisions by 11 users not shown)
Line 3:
| name = CoffeeScript
| logo = CoffeeScript-logo.svg
| paradigmparadigms = [[Multi-paradigm]]: [[prototypePrototype-based programming|prototype-based]], [[functionalFunctional programming|functional]], [[imperativeImperative programming|imperative]], [[scriptingScripting language|scripting]]
| family = [[ECMAScript]]
| released = {{start date and age|2009|12|13}}
| designer = [[Jeremy Ashkenas]]
| developer = [[Jeremy Ashkenas]]same
| released = {{startStart date and age|2009|12|13}}
| latest_release_version = {{Wikidata|property|reference|edit| Q1106819 |P348}}
| latest_release_datelatest =release {{startversion date= and age|{{Wikidata|qualifierproperty|reference|edit| Q1106819 |P348|P577}}}}
| 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]]
| programming language = CoffeeScript
| platform = [[x86-64]]
| operating system = [[Cross-platform software|Cross-platform]]
| license = [[MIT License|MIT]]
| file_extfile ext = <code>.coffee</code>, <code>.litcoffee</code>{{citation needed|date=September 2020}}
| influenced_byinfluenced 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>
| influenced = [[MoonScript]], [[LiveScript (programming language)|LiveScript]], JavaScript
| operating_system = [[Cross-platform]]
| license = [[MIT License]]
| file_ext = <code>.coffee</code>, <code>.litcoffee</code>{{citation needed|date=September 2020}}
}}
 
'''CoffeeScript''' is a [[programming language]] that compiles to [[JavaScript]]. It adds [[syntactic sugar]] inspired by [[Ruby (programming language)|Ruby]], [[Python (programming language)|Python]], and [[Haskell (programming language)|Haskell]] in an effort to enhance JavaScript's brevity and readability.<ref name="thelittlebookoncoffeescriptLittleBookMacCaw">{{cite book |last1=MacCaw |first1=Alex |date=28 February 2012 |title=The Little Book on CoffeScriptCoffeeScript: |The author=AlexJavaScript MacCawDeveloper's Guide to Building Better Web Apps |edition=1st |publisher=[[O'Reilly Media]] |language=en |isbn=978-1-4493-2105-5}}</ref><ref name="LittleBookThomas">{{cite book |last1=Thomas |first1=Lucas |date=January2017 2012|title=The Little Book on CoffeeScript |edition=1st |publisher=CreateSpace Independent Publishing Platform |language=en |isbn=978-1-44935472-21051072-5}}</ref> SpecificSome additionaladded features include [[list comprehension]] and [[destructuring assignment]].
 
CoffeeScript support is included in [[Ruby on Rails]] version 3.1<ref>{{cite web |authorlast1=Peek |first1=Josh<!--ua ?--> Peek |url=https://twitter.com/joshpeek/status/58184348742074368 |title=Tweet by Rails Core Team Member |date= April 13, 2011}}</ref> and [[Play Framework]].<ref>{{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}}</ref> In 2011, [[Brendan Eich]] referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.<ref>Eich, Brendan. "[http://brendaneich.com/2011/01/harmony-of-my-dreams/ Harmony of My Dreams]"</ref><ref>Eich, Brendan. "[http://brendaneich.com/2011/05/my-jsconf-us-presentation/ My JSConf.US Presentation]"</ref>
 
== History ==
On December 13, 2009, [[Jeremy Ashkenas]] made the first [[Git (software)|Git]] commit of CoffeeScript with the comment: "initial commit of the mystery language"."<ref>Github. [https://github.com/jashkenas/coffee-script/commit/8e9d637985d2dc9b44922076ad54ffef7fa8e9c2 'initial commit of the mystery language']</ref> 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 (compilers)|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.<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 CoffeeScript’sCoffeeScript's hallmark.".
 
== Syntax ==
Line 36 ⟶ 40:
 
<syntaxhighlight lang="javascript">
constlet mass = 72;
constlet height = 1.78;
constlet BMI = mass / height ** 2;
if (18.5 <= BMI && BMI < 25) { alert('You are healthy!') };
</syntaxhighlight>
 
Line 53 ⟶ 57:
To compute the [[greatest common divisor]] of two integers with the [[Euclidean algorithm]], in JavaScript one usually needs a ''while'' loop:
 
<syntaxhighlight lang="javascript">let gcd = (x, y) => {
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><ref>CoffeeScript calls this "[[pattern matching]]", which is a non-standard use of that term.</ref> instead:
Line 81 ⟶ 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[[ES6]] JavaScript snippet using the [[jQuery]] library is:
 
<syntaxhighlight lang="javascript">
$(document).ready(function() {
// Initialization code goes here
});
</syntaxhighlight>
 
Line 94 ⟶ 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. 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:
 
<!-- Ruby is probably the most similar language that GeSHi supports -->
Line 121 ⟶ 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:
 
<syntaxhighlight lang="coffeescript">
Line 142 ⟶ 144:
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
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 150 ⟶ 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.
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
Line 164:
// ...
function baz() {
var foo = "bar";
console.log(`foo = ${foo}`);
}
// ...
Line 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.
is limited to a block or not without looking outside the block.
 
== Development and distribution ==
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 [[Literateliterate Programmingprogramming]], using the <code>.coffee.md</code> or <code>.litcoffee</code> file extension. This allows CoffeeScriptthe [[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 ==
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://dropbox.tech/application/dropbox-dives-into-coffeescript| title=Dropbox dives into CoffeeScript| date=13 September 2012|lastlast1= Wheeler| firstfirst1=Dan| last2= Mahkovec|first2= Ziga |last3= Varenhorst |first3=Chris|access-date=11 May 2013}}</ref> however it was migrated to [[TypeScript]] in 2017.<ref>{{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}}</ref>
 
[[GitHub]]'s internal style guide once said "write new JS in CoffeeScript", though it no longer does, <ref>{{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}}</ref> and theirits [[Atom (text editor)|Atom text editor]] was also written in the language, with configuration written in [[CSON]] ("CoffeeScript Object Notation"), a variant of [[JSON]].<ref>[{{cite web |url=https://github.com/atom/atom |title=Atom source code |publisher=[[GitHub]]. github|access-date=2021-06-26}}</ref><ref>{{cite web |title=Basic Customization |url=https://flight-manual.comatom-editor.cc/using-atom/sections/basic-customization/#configuring-with-cson Retrieved|website=[[Atom on(text 2021editor)|Atom]] Flight Manual |publisher=[[GitHub]] |access-06date=29 April 2024 |language=en |url-26status=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}}</ref>
 
[[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=PIXELPixel GAMEGame MAKERMaker MV (PC) |url=https://www.christcenteredgamer.com/reviews/pc-mac/pixel-game-maker-mv-pc |website=Christ Centered Gaming |access-date=15 January 2021}}</ref>
 
== See also ==
Line 202:
* [[Dart (programming language)]]
* [[Kotlin (programming language)]]
* [[LiveScript (programming language)|LiveScript]]
* [[Opa (programming language)]]
* [[Elm (programming language)]]
Line 209:
 
== References ==
{{Reflist|30em}}
 
== Further reading ==
* {{Cite book |last1=Lee |first1=Patrick |date=May 14, 2014 |title=CoffeeScript in Action |edition=First1st |publisher=[[Manning Publications]] |page=432 |isbn=978-1617290626}}
* {{Cite journalbook |last1=Grosenbach |first1=Geoffrey |date=May 12, 2011 |title=Meet CoffeeScript |edition=First1st |publisher=[[PeepCode]]}}
* {{Cite book |last1=Bates |first1=Mark |date=May 31, 2012 |title=Programming in CoffeeScript |edition=First1st |publisher=[[Addison-Wesley]] |page=350 |isbn=978-0-321-82010-5}}
* {{Cite book |last1=MacCawBurnham |first1=AlexTrevor |date=JanuaryAugust 313, 20122011 |title=TheCoffeeScript: LittleAccelerated BookJavaScript onDevelopment CoffeeScript|edition=First1st |publisher=[[O'ReillyPragmatic MediaBookshelf]] |page=62[https://archive.org/details/isbn_9781934356784/page/138 138] |isbn=978-14493210551934356784 |url-access=registration |url=https://archive.org/details/isbn_9781934356784/page/138}}
* {{Cite book|last1=Burnham|first1=Trevor|date=August 3, 2011|title=CoffeeScript: Accelerated JavaScript Development|edition=First|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}}
 
== External links ==
* {{Official website}}
 
{{JavaScript|state=collapsed}}
{{Programming languages}}
{{JavaScript|state=collapsed}}
{{NodeJs}}
{{Programming languages}}
{{Authority control}}
 
Line 235 ⟶ 234:
[[Category:2009 software]]
[[Category:Free software projects]]
<!-- Hidden categories below -->
[[Category:Articles with example JavaScript code]]