请输入您要查询的百科知识:

 

词条 CoffeeScript
释义

  1. History

  2. Syntax

  3. Examples

      Interval test    Loops and comprehensions    Functions and jQuery    String interpolation  

  4. Compiling

  5. Latest additions

  6. Extensions

  7. Adoption

  8. Criticism

  9. See also

      Other languages that compile to JavaScript  

  10. References

  11. Further reading

  12. External links

{{Infobox programming language
| name = CoffeeScript
| logo =
| paradigm = Multi-paradigm: prototype-based, functional, imperative, scripting
| released = {{start date and age|2009|12|13|df=yes}}
| designer = Jeremy Ashkenas
| developer = Jeremy Ashkenas
| latest_release_version = 2.4.0
| latest_release_date = {{start date and age|2019|03|29|df=yes}}[1]
| influenced_by = Haskell, JavaScript, Perl,{{citation needed|date=January 2016}} Python,[2] Ruby, YAML[3]
| influenced = MoonScript, LiveScript, JavaScript
| operating_system = Cross-platform
| license = MIT License
| file_ext = .coffee, .litcoffee
| website = {{URL|coffeescript.org}}
}}

CoffeeScript is a programming language that transcompiles 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 pattern matching.

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]

History

On 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."

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 / Math.pow(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

Loops and comprehensions

To compute the greatest common divisor of two integers with the euclidean algorithm, in JavaScript one usually needs a while loop:

gcd = (x, y) => {

  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 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 for ... in syntax allows looping over arrays while the for ... of syntax allows looping over objects.

The ? keyword quickly checks if a variable is null or undefined :

personCheck = ->

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() {

})

Or even just:

$(function() {

})

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, using indentation level instead to denote a function or block. Thus, the CoffeeScript equivalent of the snippet above is:

$(document).ready ->

Or just:

$ ->

String interpolation

Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #{ ... }, and single-quoted strings are literal.[13]

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.[14] One alternative to the Node.js utility is the [https://github.com/talios/coffee-maven-plugin 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[15] site provides bi-directional translation.

Latest additions

  • Source maps allow users to de-bug their CoffeeScript code directly, supporting CoffeeScript tracebacks on run time errors.
  • CoffeeScript supports a form of Literate Programming, using the .coffee.md or .litcoffee file extension. This allows CoffeeScript 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

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

Adoption

On September 13, 2012, Dropbox announced that their browser-side code base has been rewritten from JavaScript to CoffeeScript.[17]

GitHub's internal style guide once said "write new JS in CoffeeScript", and while it no longer does, all the advice in the style guide references how to write good CoffeeScript,[18] and their Atom text editor is also written in the language.[19]

Criticism

CoffeeScript has been criticized for its unusual scoping

rules[20][21]. 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 snipped in JavaScript

one does not have to look outside the {}-block to know for

sure that no possible foo variable in the outer scope can be

incidentally overriden:

  // ...  {    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.

See also

  • Source-to-source compiler

Other languages that compile to JavaScript

{{Portal|Free and open-source software}}

(In chronological order, with ones listed in the TIOBE index highlighted in bold.)

  • Haxe (2006): a language that can be transpiled to C++, Java, C#, Python, Lua, PHP, and ActionScript; as well as JavaScript.
  • Nim (2008): a statically-typed programming language with syntactical similarities to Python. The same Nim code can be compiled to C/C++ (optimized systems programming, server-side, etc) or to JavaScript (interpreted scripting, client-side).
  • LiveScript (2011): an indirect descendant of CoffeeScript focusing on functional programming.
  • Amber Smalltalk (2011): an implementation of the Smalltalk-80 language that runs on the JavaScript runtime.
  • Dart (2011): a Google-led general-purpose OOP language with optional typing.
  • Opa (2011): an integrated stack for developing scalable client-server Web applications.
  • TypeScript (2012): a Microsoft-led strict superset of JavaScript with optional typing.
  • Elm (2012): a statically-typed purely-functional language that compiles to JavaScript.

References

1. ^https://github.com/jashkenas/coffeescript/releases
2. ^http://coffeescript.org/ "CoffeeScript borrows chained comparisons from Python"
3. ^{{cite news|url=http://www.javaworld.com/javaworld/jw-10-2011/111018-coffeescript-vs-dart.html|title=Turn up your nose at Dart and smell the CoffeeScript|last=Heller|first=Martin|date=18 October 2011|work=JavaWorld|publisher=InfoWorld|accessdate=2012-02-09}}
4. ^{{cite book | title=The Little Book on CoffeScript | author=Alex MacCaw | publisher=O'Reilly Media |date=January 2012 | isbn=978-1-4493-2105-5}}
5. ^{{cite web |author=Josh Peek |url=https://twitter.com/joshpeek/status/58184348742074368 |title=Tweet by Rails Core Team Member |date= April 13, 2011}}
6. ^{{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}}
7. ^Eich, Brendan. "Harmony of My Dreams"
8. ^Eich, Brendan. "My JSConf.US Presentation"
9. ^Github. [https://github.com/jashkenas/coffee-script/commit/8e9d637985d2dc9b44922076ad54ffef7fa8e9c2 'initial commit of the mystery language']
10. ^Hacker News. [https://news.ycombinator.com/item?id=2037801 CoffeeScript 1.0.0 announcement] posted by Jeremy Ashkenas on Dec 24, 2010
11. ^Hacker News. [https://news.ycombinator.com/item?id=1014080 Original CoffeeScript announcement] posted by Jeremy Ashkenas on Dec 24, 2009
12. ^coffeescript.org Announcing CoffeeScript 2
13. ^{{cite web|title=Official CoffeeScript Page|url=http://coffeescript.org|accessdate=20 November 2013}}
14. ^[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.
15. ^{{cite web |url=http://js2.coffee |title=js2coffee|last=Sta Cruz|first= Rico|accessdate=11 May 2014}}
16. ^{{cite web|url=http://maxtaco.github.io/coffee-script/ |title=Official IcedCoffeeScript website |}}
17. ^{{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}}
18. ^{{cite web|url=https://github.com/styleguide/javascript |title=JavaScript · Styleguide · GitHub |publisher=Github.com |date= |accessdate=2015-11-30|archiveurl=https://web.archive.org/web/20130815075924/https://github.com/styleguide/javascript|archivedate=2013-08-15|deadurl=no}}
19. ^[https://github.com/atom/atom Atom source code]. github.com. Retrieved on 2015-07-22.
20. ^{{cite web|url=http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/|title=The Problem with Implicit Scoping in CoffeeScript|accessdate=2018-10-13}}
21. ^{{cite web|url=https://donatstudios.com/CoffeeScript-Madness|title=CoffeeScript's Scoping is Madness|accessdate=2018-10-13}}

Further reading

  • {{Cite journal|last1=Lee|first1=Patrick|date=May 14, 2014|title=CoffeeScript in Action|journal=|edition=First|publisher=Manning Publications|volume=|page=432|pages=|isbn=978-1617290626|via=}}
  • {{Cite journal|last1=Grosenbach|first1=Geoffrey|date=May 12, 2011|title=Meet CoffeeScript|journal=|edition=First|publisher=PeepCode|volume=|pages=|postscript=|via=}}
  • {{Cite journal|last1=Bates|first1=Mark|date=May 31, 2012|title=Programming in CoffeeScript|journal=|edition=First|publisher=Addison-Wesley|volume=|page=350|pages=|isbn=0-321-82010-X|via=}}
  • {{Cite journal|last1=MacCaw|first1=Alex|date=January 31, 2012|title=The Little Book on CoffeeScript|journal=|edition=First|publisher=O'Reilly Media|volume=|page=62|pages=|isbn=978-1449321055|via=}}
  • {{Cite journal|last1=Burnham|first1=Trevor|date=August 3, 2011|title=CoffeeScript: Accelerated JavaScript Development|journal=|edition=First|publisher=Pragmatic Bookshelf|volume=|page=138|pages=|isbn=978-1934356784|via=}}

External links

  • {{Official website}}
{{Programming languages}}{{JavaScript|state=collapsed}}{{NodeJs}}

9 : Dynamic programming languages|Programming languages created in 2009|JavaScript programming language family|Prototype-based programming languages|Software using the MIT license|Source-to-source compilers|High-level programming languages|2009 software|Free software projects

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 23:22:22