CoffeeScript ( ['kɔ: fɪ skrɪpt] ; coffeeie script ) is a programming language that translates into JavaScript . CoffeeScript adds Ruby , Python , Haskell, and Erlang syntactic sugar to improve code readability and size. CoffeeScript allows you to write more compact code compared to JavaScript [3] . The JavaScript code received by translation from CoffeeScript is fully validated by JavaScript Lint .
| CoffeeScript | |
|---|---|
| Language class | multiparadigmal : object oriented , imperative , functional , aspect oriented , prototype |
| Appeared in | |
| Author | Jeremy Ashkenas |
| File extension | or |
| Release | |
| Influenced | JavaScript , Python , Ruby , Haskell , Erlang |
| Influenced | MoonScript , LiveScript |
| License | |
| Site | |
| OS | |
History
The creator of the language is Jeremy Ashkenas .
Initially, the compiler was written in Ruby , but in version 0.5, which was released on February 21, 2010 , the compiler was implemented on CoffeeScript itself.
CoffeeScript has been welcomed in the Ruby community. Native CoffeeScript support has been added to the Ruby on Rails web framework since version 3.1.
Implementation
On the official website of the language there is a section “try coffeescript” that allows you to run programs on it online [4] . Unlike, for example, Try Ruby [5] , there will be no server requests, the code is compiled and executed directly in the browser.
Examples
Variables
CoffeeScript:
age = 2
male = true
name = "Matvey"
JavaScript:
var age = 2 ,
male = true
name = "Matvey" ;
Functions
CoffeeScript:
say = ( speech ) ->
alert speech
say "Hello world!"
JavaScript using ECMAScript 2015:
const say = speech => alert ( speech );
say ( 'Hello world!' );
JavaScript:
var say = function ( speech ) {
alert ( speech );
};
say ( "Hello world!" );
Classes and Objects
CoffeeScript:
class Human
constructor: (@name) ->
class Baby extends Human
say: (msg) -> alert " # { @name } says ' # { msg } '"
sayHi: -> @say ( 'hello!' )
matt = new Baby ( "Matvey" )
matt . sayHi ()
JavaScript using ECMAScript 2015:
class Human {
constructor ( name ) {
this . name = name ;
}
}
class Baby extends Human {
say ( msg ) {
alert ( ` $ { this . name } says ' $ { msg } '` );
}
sayHi () {
this . say ( 'hello!' );
}
}
const matt = new Baby ( 'Matvey' );
matt . sayHi ();
An analogue in JavaScript (an analogue, not a compilation result):
function Human ( name ) {
this . name = name ;
}
function Baby ( name ) {
Human call ( this , name );
}
Baby prototype = Object . create ( Human . prototype );
Baby prototype . say = function ( msg ) {
alert ( this . name + 'says' + msg );
};
Baby prototype . sayHi = function () {
this . say ( 'hello!' );
};
Baby prototype . constructor = Human ;
var matt = new Baby ( "Matvey" );
matt . sayHi ();
Note: in JavaScript, when working with “classes” (constructor + prototypes + functions for inheritance and mixing), they often use wrappers (MooTools, AtomJS and others). JavaScript analogy with AtomJS class wrapper:
var Human = Class ({
initialize : function ( name ) {
this . name = name ;
}
});
var Baby = Class ({
Extends : Human ,
say : function ( msg ) {
alert ( this . name + 'says' + msg );
},
sayHi : function () {
this . say ( 'hello!' );
}
});
var matt = new Baby ( "Matvey" );
matt . sayHi ();
An example CoffeeScript class with various kinds of properties.
class test
say = (msg) -> alert msg # private method
@echo = (msg) -> console . log msg # static method written to Test
setHi: (msg) -> # dynamic method, recorded in Test.prototype
@hi = -> msg # dynamic method, written to instance Test
See also
- Dart
- Haxe
- Javascript
- TypeScript
Notes
- ↑ Release 2.4.1 - 2019.
- ↑ The coffeescript Open Source Project on Open Hub: Licenses Page - 2006.
- ↑ Example on the cover page of the official website
- ↑ Try CoffeeScript . coffeescript.org. Date of treatment January 4, 2016.
- ↑ Try Ruby: learn the basics of the Ruby language in your browser. . tryruby.org. Date of treatment January 4, 2016.
Literature
- Mark Bates. CoffeeScript Second Wind JavaScript = Mark Bates. Programming in CoffeeScript. - M .: DMK , 2012 .-- 312 p. - 300 copies. - ISBN 978-5-94074-842-7 .
- Alex MacCaw. The Little Book on CoffeeScript . - O'Reilly Media, 2011 .-- 60 p. - ISBN 9781449321055,.
- CoffeeScript Cookbook , a collection of CoffeeScript recipes from the community.
- Smooth CoffeeScript , a free e-book about CoffeeScript. Available in two versions with source code examples.
- Michael Galpin. Your first cup of CoffeeScript: Part 1. Getting started . DeveloperWorks (July 19, 2012). Date of treatment January 10, 2016.
- Your first cup of CoffeeScript: Part 2. Learning the language with practical examples (July 18, 2012). Date of treatment January 10, 2016.
- Your first cup of CoffeeScript: Part 3. Using Client-side CoffeeScript (July 20, 2012). Date of treatment January 10, 2016.
- Your First Cup of CoffeeScript: Part 4. Using Server-Side CoffeeScript (July 20, 2012). Date of treatment January 10, 2016.
- Andrew Glover. Functional JavaScript using CoffeeScript and Node . DeveloperWorks (December 3, 2012). Date of treatment January 10, 2016.
Links
- CoffeeScript official website
- CoffeeScript repository on GitHub
- Translation of official documentation
- CoffeeScript Russian-speaking group
- Russian site by CoffeeScript
- Video course (en)
Related projects:
- CoffeeScript Compiler for Windows
- CoffeeKup , a template engine and engine for generating HTML code in CoffeeScript.
- Prepros , compiles CoffeeScript on the fly. For Windows and OSX (also compiles LESS, Stylus, Haml, Jade, Markdown, Slim, SASS)