CoffeeScript: Difference between revisions
Updated version Tags: Mobile edit Mobile web edit |
→Adoption: Fixed typo Tags: Mobile edit Mobile web edit |
||
Line 171: | Line 171: | ||
On September 13, 2012, [[Dropbox (service)|Dropbox]] announced that their browser-side codebase has been rewritten from [[JavaScript]] to CoffeeScript.<ref>{{cite web|url=https://tech.dropbox.com/?p=361| title=Dropbox dives into CoffeeScript| date=13 September 2012|last= Wheeler| first=Dan| last2= Mahkovec|first2= Ziga |last3= Varenhorst |first3=Chris|accessdate=11 May 2013}}</ref> |
On September 13, 2012, [[Dropbox (service)|Dropbox]] announced that their browser-side codebase has been rewritten from [[JavaScript]] to CoffeeScript.<ref>{{cite web|url=https://tech.dropbox.com/?p=361| title=Dropbox dives into CoffeeScript| date=13 September 2012|last= Wheeler| first=Dan| last2= Mahkovec|first2= Ziga |last3= Varenhorst |first3=Chris|accessdate=11 May 2013}}</ref> |
||
[[GitHub]]'s internal style guide says "write new JS in CoffeeScript" |
[[GitHub]]'s internal style guide says "write new JS in CoffeeScript",<ref>{{cite web|url=https://github.com/styleguide/javascript |title=JavaScript · Styleguide · GitHub |publisher=Github.com |date= |accessdate=2014-05-15}}</ref> and their [[Atom_(text_editor) | Atom text editor]] is also written in the language. |
||
== See also == |
== See also == |
Revision as of 06:55, 6 July 2015
This article needs to be updated.(September 2013) |
File:CoffeeScript-logo.png | |
Paradigm | Multi-paradigm: prototype-based, functional, imperative, scripting |
---|---|
Designed by | Jeremy Ashkenas |
Developer | Jeremy Ashkenas, et al. |
First appeared | December 13, 2009 |
Stable release | 1.9.3
/ May 14, 2015 |
OS | Cross-platform |
License | MIT License |
Filename extensions | .coffee |
Website | coffeescript |
Influenced by | |
Haskell,[1] JavaScript, Perl, Python,[1] Ruby,[1] YAML[2] | |
Influenced | |
MoonScript, LiveScript |
CoffeeScript is a programming language that transcompiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python and Haskell[1] to enhance JavaScript's brevity and readability.[3] Specific additional features include list comprehension and pattern matching.
The language has a relatively large following[citation needed] in the Ruby community. CoffeeScript support is included in Ruby on Rails version 3.1.[4] In 2011, Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.[5][6]
History
On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment: "initial commit of the mystery language."[7] 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.[8][9]
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, these control statements also have postfix versions; for example, if
can also be written after the conditional statement.
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.
Examples
Interval test
To compute the body mass index, one may do (here in JavaScript):
var mass = 72
var height = 1.78
var BMI = mass / (height * height)
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
Loops and comprehensions
To compute the greatest common divisor of two integers with the euclidean algorithm, one usually needs a while loop:
function gcd(x, y) {
var z
do {
z = x % y
x = y
y = z
} while (y != 0)
return x
}
Whereas in CoffeeScript one can use until
and pattern-matching instead:
gcd = (x, y) ->
[x, y] = [y, x%y] until y is 0
x
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 which 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
You can implement a linear search 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 you to loop over arrays while the for ... of
syntax allows you to loop over objects.
You can use the ?
keyword to quickly check 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.
Functions and jQuery
A common 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.[dubious – discuss] Thus, the CoffeeScript equivalent of the snippet above is
$(document).ready ->
# Initialization code goes here
Or just
$ ->
# Initialization code goes here
String Interpolation
Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #{ ... }, and single-quoted strings are literal.[10]
author = "Wittgenstein"
quote = "A picture is a fact. -- #{ author }"
sentence = "#{ 22 / 7 } is a decent approximation of π"
Compiling
The CoffeeScript compiler has been written in CoffeeScript 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.[11] One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven build system. The plugin uses the Rhino JavaScript engine written in Java.
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[12] site provides bi-directional translation.
Issues
- CoffeeScript is very sensitive to whitespace, allowing implicit function calls and object literals, as well as enforcing significant indentation.
- CoffeeScript traditionally compiled to ES3, making it impossible to support some recently added JavaScript features, though this policy has been relaxed (generator support as of CoffeeScript 1.9.0).
- Priority rules are not always intuitive, meaning new users can be confused by the resolution of certain subexpressions.
- The claim of increased source code readability is disputed, as CoffeeScript does allow for relatively cryptic coding styles.
- The compiled code is often more difficult to read than natural JavaScript.
The above issues are only indicative and there are many other strong arguments for and against CoffeeScript.[13]
Latest additions
- Source maps allow users to debug their CoffeeScript code directly, supporting CoffeeScript tracebacks on runtime errors.
- CoffeeScript supports a form of Literate Programming, using the .coffee.md file extension. This allows CoffeeScript source code to be written in Markdown.
Adoption
On September 13, 2012, Dropbox announced that their browser-side codebase has been rewritten from JavaScript to CoffeeScript.[14]
GitHub's internal style guide says "write new JS in CoffeeScript",[15] and their Atom text editor is also written in the language.
See also
- Amber Smalltalk
- Haxe
- Dart
- Opa
- TypeScript, an open-source, strict superset of Javascript language from Microsoft.
- RapydScript,[16] a translator with a more Pythonic syntax
- LiveScript
- GorillaScript, which offers additional type checking and syntactic sugar for common JS patterns such as inline callbacks
References
- ^ a b c d The Changelog. Episode 0.2.9 - CoffeeScript with Jeremy Ashkenas, July 23, 2010
- ^ Heller, Martin (18 October 2011). "up your nose at Dart and smell the CoffeeScript". JavaWorld. InfoWorld. Retrieved 2012-02-09.
- ^ Alex MacCaw (January 2012). The Little Book on CoffeScript. O'Reilly Media. ISBN 978-1-4493-2105-5.
- ^ , Joshua. Tweet by Rails Core Team Member on Apr 13, 2011
- ^ Eich, Brendan. "Harmony of My Dreams"
- ^ Eich, Brendan. "My JSConf.US Presentation"
- ^ Github. 'initial commit of the mystery language'
- ^ Hacker News. CoffeeScript 1.0.0 announcement posted by Jeremy Ashkenas on Dec 24, 2010
- ^ Hacker News. Original CoffeeScript announcement posted by Jeremy Ashkenas on Dec 24, 2009
- ^ "Official CoffeeScript Page". Retrieved 20 November 2013.
- ^ CoffeeScript. Jashkenas.github.com. Retrieved on 2013-07-21.
- ^ Sta Cruz, Rico. "js2coffee". Retrieved 11 May 2014.
- ^ Way, Jeffrey (14 Dec 2011). "Should you learn CoffeeScript". tuts+. Retrieved 11 May 2014.
- ^ Wheeler, Dan; Mahkovec, Ziga; Varenhorst, Chris (13 September 2012). "Dropbox dives into CoffeeScript". Retrieved 11 May 2013.
- ^ "JavaScript · Styleguide · GitHub". Github.com. Retrieved 2014-05-15.
- ^ "Rapydscript bitbucket repository". Atlassian Bitbucket. 3 Apr 2013. Retrieved 11 May 2014.
Further reading
- Lee, Patrick (May 14, 2014). "CoffeeScript in Action" (First ed.). Manning Publications: 432. ISBN 978-1617290626.
{{cite journal}}
: Cite journal requires|journal=
(help) - Grosenbach, Geoffrey (May 12, 2011). "Meet CoffeeScript" (First ed.). PeepCode.
{{cite journal}}
: Cite journal requires|journal=
(help) - Bates, Mark (May 31, 2012). "Programming in CoffeeScript" (First ed.). Addison-Wesley: 350. ISBN 0-321-82010-X.
{{cite journal}}
: Cite journal requires|journal=
(help) - MacCaw, Alex (January 31, 2012). "The Little Book on CoffeeScript" (First ed.). O'Reilly Media: 62. ISBN 978-1449321055.
{{cite journal}}
: Cite journal requires|journal=
(help) - Burnham, Trevor (August 3, 2011). "CoffeeScript: Accelerated JavaScript Development" (First ed.). Pragmatic Bookshelf: 138. ISBN 978-1934356784.
{{cite journal}}
: Cite journal requires|journal=
(help)
External links
- Official website
- coffee-script on GitHub
- CoffeeScript syntax definition to highlight your CoffeeScript code in Crucible of Atlasssian by Tomás Corral