CoffeeScript
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.[4] Specific additional features include list comprehension and destructuring assignment. CoffeeScript support is included in Ruby on Rails version 3.1[5] and Play Framework.[6] In 2011, Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.[7][8] HistoryOn December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment "initial commit of the mystery language".[9] 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.[10][11] On September 18, 2017, version 2.0.0 was introduced,[12] 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". SyntaxAlmost everything is an expression in CoffeeScript, for example, 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 gcd = (x, y) ->
[x, y] = [y, x%y] until y is 0
x
The 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 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 $(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.[14] 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 CoffeeScript has been criticized for its unusual scoping rules.[15][16] 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 // ...
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 distributionThe 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.[17] 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] 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[18] site provides bi-directional translation. Latest additions
ExtensionsIced CoffeeScript is a superset of CoffeeScript which adds two new keywords: AdoptionOn September 13, 2012, Dropbox announced that their browser-side code base had been rewritten from JavaScript to CoffeeScript,[20] however it was migrated to TypeScript in 2017.[21] GitHub's internal style guide once said "write new JS in CoffeeScript", though it no longer does,[22] and their Atom text editor was also written in the language, with configuration written in CSON ("CoffeeScript Object Notation"), a variant of JSON.[23][24] Pixel Game Maker MV makes uses of CoffeeScript as part of its game development environment.[25] See also
References
Further reading
External links |