Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

CoffeeScript

From Wikipedia, the free encyclopedia
Programming language which compiles to JavaScript
CoffeeScript
ParadigmsMulti-paradigm:prototype-based,functional,imperative,scripting
FamilyECMAScript
Designed byJeremy Ashkenas
Developersame
First appearedDecember 13, 2009; 16 years ago (2009-12-13)
Stable release
2.7.0[1] Edit this on Wikidata / 24 April 2022; 3 years ago (24 April 2022)
Typing disciplinedynamic,implicit
Scopelexical
Implementation languageCoffeeScript
Platformx86-64
OSCross-platform
LicenseMIT
Filename extensions.coffee,.litcoffee[citation needed]
Websitecoffeescript.org
Influenced by
Haskell,JavaScript,Perl,[citation needed]Python,[2]Ruby,YAML[3]
Influenced
MoonScript,LiveScript, JavaScript

CoffeeScript is aprogramming language that compiles toJavaScript. It addssyntactic sugar inspired byRuby,Python, andHaskell in an effort to enhance JavaScript's brevity and readability.[4][5] Some added features includelist comprehension anddestructuring assignment.

CoffeeScript support is included inRuby on Rails version 3.1[6] andPlay Framework.[7] In 2011,Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.[8][9]

History

[edit]

On December 13, 2009,Jeremy Ashkenas made the firstGit commit of CoffeeScript with the comment "initial commit of the mystery language".[10] 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 aself-hosting version in pure CoffeeScript. By that time the project had attracted several other contributors onGitHub, and was receiving over 300 page hits per day.

On December 24, 2010, Ashkenas announced the release of stable 1.0.0 toHacker News, the site where the project was announced for the first time.[11][12]

On September 18, 2017, version 2.0.0 was introduced,[13] 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

[edit]

Almost everything is anexpression in CoffeeScript, for example,if,switch andfor expressions (which have no return value in JavaScript) return a value. As inPerl 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 thebody mass index inJavaScript, one could write:

letmass=72;letheight=1.78;letBMI=mass/height**2;if(18.5<=BMI&&BMI<25)alert('You are healthy!');

With CoffeeScript the interval is directly described:

mass=72height=1.78BMI=mass/height**2alert'You are healthy!'if18.5<=BMI<25

To compute thegreatest common divisor of two integers with theEuclidean algorithm, in JavaScript one usually needs awhile loop:

letgcd=(x,y)=>{do{[x,y]=[y,x%y];}while(y!==0)returnx;}

Whereas in CoffeeScript one can useuntil[14] instead:

gcd=(x, y) ->[x,y]=[y,x%y]untilyis0x

The? keyword quickly checks if a variable isnull orundefined :

personCheck=->ifnotperson?thenalert("No person")elsealert("Have person")person=nullpersonCheck()person="Ivan"personCheck()

This would alert "No person" if the variable isnull orundefined and "Have person" if there is something there.

A common pre-ES6 JavaScript snippet using thejQuery library is:

$(document).ready(function(){// Initialization code goes here});

Or even just:

$(function(){// Initialization code goes here});

In CoffeeScript, thefunction keyword is replaced by the-> symbol, and indentation is used instead of curly braces, as in otheroff-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.[15]

author="Wittgenstein"quote="A picture is a fact. --#{author}"sentence="#{22/7} is a decent approximation of π"

Anyfor loop can be replaced by alist 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:

alertn*nfornin[1..10]whenn%2is1

Alternatively, there is:

alertn*nfornin[1..10]by2

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

names=["Ivan","Joanna","Nikolay","Mihaela"]linearSearch=(searchName) ->alert(name)fornameinnameswhennameissearchName

Thefor ... in syntax allows looping over arrays while thefor ... of syntax allows looping over objects.

CoffeeScript has been criticized for its unusualscoping rules.[16][17] In particular, it completely disallowsvariable shadowing which makes reasoning about code more difficult and error-prone in some basic programming patterns establishedby and taken for granted sinceprocedural programming principles were defined.

For example, with the following code snippet in JavaScriptone does not have to look outside the{}-block to know forsure that no possiblefoo variable in the outer scope can beincidentally overridden:

// ...functionbaz(){varfoo="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

[edit]

The CoffeeScript compiler has beenself-hosting since version 0.5 and is available as aNode.js utility; however, the core compiler does not rely on Node.js and can be run in anyJavaScript environment.[18] One alternative to theNode.js utility is the Coffee Maven Plugin, a plugin for theApache Maven build system. The plugin uses theRhino JavaScript engine written inJava.[citation needed]

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[19] site provides bi-directional translation.

Latest additions

[edit]
icon
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(April 2024) (Learn how and when to remove this message)
  • Source maps allow users to debug their CoffeeScript code directly, supporting CoffeeScript tracebacks on run time errors.
  • CoffeeScript supports a form ofliterate programming, using the.coffee.md or.litcoffee file extension. This allows thesource code to be written inMarkdown. The compiler will treat any indented blocks (Markdown's way of indicating source code) as code, and ignore the rest as comments.

Extensions

[edit]

Iced CoffeeScript is a superset of CoffeeScript which adds two new keywords:await anddefer. These additions simplify asynchronous control flow, making the code look more like aprocedural programming language, eliminating the call-back chain. It can be used on the server side and in the browser.[20]

Adoption

[edit]

On September 13, 2012,Dropbox announced that their browser-side code base had been rewritten fromJavaScript to CoffeeScript,[21] however it was migrated toTypeScript in 2017.[22]

GitHub's internal style guide once said "write new JS in CoffeeScript", though it no longer does,[23] and itsAtom text editor was also written in the language, with configuration written inCSON ("CoffeeScript Object Notation"), a variant ofJSON.[24][25]

Pixel Game Maker MV makes uses of CoffeeScript as part of its game development environment.[26]

See also

[edit]

References

[edit]
  1. ^"2.7.0". 24 April 2022. Retrieved9 August 2022.
  2. ^https://coffeescript.org/ "CoffeeScript borrows chained comparisons from Python"
  3. ^Heller, Martin (October 18, 2011)."Turn up your nose at Dart and smell the CoffeeScript".InfoWorld. Retrieved2020-07-15.
  4. ^MacCaw, Alex (28 February 2012).The Little Book on CoffeeScript: The JavaScript Developer's Guide to Building Better Web Apps (1st ed.).O'Reilly Media.ISBN 978-1-4493-2105-5.
  5. ^Thomas, Lucas (2017).The Little Book on CoffeeScript (1st ed.). CreateSpace Independent Publishing Platform.ISBN 978-1-5472-1072-5.
  6. ^Peek, Josh (April 13, 2011)."Tweet by Rails Core Team Member".
  7. ^"AssetsCoffeeScript - 2.5.x".www.playframework.com. Retrieved2016-10-31.
  8. ^Eich, Brendan. "Harmony of My Dreams"
  9. ^Eich, Brendan. "My JSConf.US Presentation"
  10. ^Github.'initial commit of the mystery language'
  11. ^Hacker News.CoffeeScript 1.0.0 announcement posted by Jeremy Ashkenas on Dec 24, 2010
  12. ^Hacker News.Original CoffeeScript announcement posted by Jeremy Ashkenas on Dec 24, 2009
  13. ^coffeescript.orgAnnouncing CoffeeScript 2
  14. ^CoffeeScript calls this "pattern matching", which is a non-standard use of that term.
  15. ^"Official CoffeeScript Page". Retrieved20 November 2013.
  16. ^"The Problem with Implicit Scoping in CoffeeScript". Retrieved2018-10-13.
  17. ^"CoffeeScript's Scoping is Madness". 25 July 2013. Retrieved2018-10-13.
  18. ^CoffeeScriptArchived 2012-04-27 at theWayback Machine. Jashkenas.github.com. Retrieved on 2013-07-21.
  19. ^Sta Cruz, Rico."js2coffee". Retrieved11 May 2014.
  20. ^"Official IcedCoffeeScript website".
  21. ^Wheeler, Dan; Mahkovec, Ziga; Varenhorst, Chris (13 September 2012)."Dropbox dives into CoffeeScript". Retrieved11 May 2013.
  22. ^Goldstein, David (13 May 2020)."The Great CoffeeScript to Typescript Migration of 2017".Dropbox.Tech. Retrieved30 June 2020.
  23. ^"JavaScript · Styleguide · GitHub". Github.com. Archived fromthe original on 2013-08-15. Retrieved2015-11-30.
  24. ^"Atom source code".GitHub. Retrieved2021-06-26.
  25. ^"Basic Customization".Atom Flight Manual.GitHub.Archived from the original on 2024-04-29. Retrieved29 April 2024.
  26. ^Cullen, Daniel."Pixel Game Maker MV (PC)".Christ Centered Gaming. Retrieved15 January 2021.

Further reading

[edit]
Code analysis
Subsets,* supersets
Transpilers
Concepts
Debuggers
Documentation generators
Editors (comparison)
Engines
Frameworks
Relatedtechnologies
Package managers
Module bundlers
Server-side
Unit testing frameworks (list)
People
Platform
Packaging
Frameworks
Libraries
Languages
National
Other
Retrieved from "https://en.wikipedia.org/w/index.php?title=CoffeeScript&oldid=1334492204"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp