📜 ⬆️ ⬇️

Coffeescript - Javascript Powered by Ruby

CoffeeScript is a language to write in JavaScript with a more convenient syntax.

A brief CoffeeScript example (with jQuery):
jQuery($ =>
notified: false
$( 'a' ).click( =>
if notified
true
else
$( '.alert' ).show()
false ..
).
)

This code will be translated to:
jQuery( function ($) {
var notified = false ;
$( 'a' ).click( function () {
if (notified) {
return true ;
} else {
$( '.alert' ).show()
return false ;
}
})
})


Introduction


JavaScript is a great language: a good ideology of prototype OOP, great virtual machines like V8 and a great future for the king of web clients. With the spread of HTML 5, it will start cramping Flash and Silverlight. And recently, JS began to be considered as a server language (for example, node.js ).

Only in the modern world of syntactic sugar, JavaScript code is a bit heavy. With each lambda it is wrong to write a long line function() { … } , there is not enough switch-when and foreach for arrays 1 . After Ruby and Python, even parentheses around the condition in the if or required return appear to be superfluous characters.
')
That would be the same JavaScript, but with a slightly different syntax. And we have successful examples of changing the syntax: Haml and Sass for HTML and CSS, respectively. They do not hide from the layout (as terrible WebForms), but simply allow you to write the same code with a slightly different syntax, but we constantly monitor the result of the broadcast.

CoffeeScript


CoffeeScript for JavaScript, like Haml for HTML. Allows you to write the same code, a little differently:
Operators

The if and while statements have extra characters removed:
while a < 20
a += 1.

And there is also a short form for one line.
student.passed(exam.subject) if exam.mark > 2

Lambda

Simplifies the lambda syntax. In CoffeeScript, like Ruby, any function returns the last value.
square: x =>
x * x.
square(2)

Arrays

Allows you to work with arrays as in Python:
# codes ['toast', 'cheese', 'wine']
codes: food.toLowerCase() for food in [ 'Toast' , 'Cheese' , 'Wine' ].
#
highlight(row) for row, i in table if i % 2 == 0.

And it makes it easier to get a substring or part of an array:
"abcdef" [0..3]

numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
three_to_six: numbers[3..6]
.


More information can be found on the site CoffeeScript .

Instruments


CoffeeScript translator is written in Ruby and distributed through RubyGems:
  gem install coffee-script 

Using it, you can translate your .coffee files into regular JavaScript code:
  coffee application.coffee 

In order not to forget to translate the file and not to look for a ghostly error, you can specify to automatically translate all .coffee files when changing:
  coffee -w * .coffee 

By installing Narwhal, you can experiment with CoffeeScript in an interactive console or execute coffee scripts outside the browser.

Notes


1 - In JavaScript 1.6 there is Array#forEach() , and in jQuery there is $.each() , but they work slower and the long lambda still kills them.

Source: https://habr.com/ru/post/79586/


All Articles