Coffeescript - Ruby Force Javascript
CoffeeScript is a language for writing in JavaScript with a more convenient syntax.
A brief CoffeeScript example (with jQuery):
This code will be translated to:
JavaScript is a great language: a good ideology for the 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 begin to push Flash and Silverlight. And recently, JS began to be considered as a server language (for example, node.js ).
That's just in the modern world of syntactic sugar JavaScript code is a bit heavy. With each lambda it’s wrong to write a long string
That would be the same JavaScript, but with a slightly different syntax. And we have successful examples of syntax changes: Haml and Sassfor HTML and CSS, respectively. They do not hide from the layout (like terrible WebForms), but simply allow you to write the same code with a slightly different syntax, but we constantly monitor the translation result.
CoffeeScript for JavaScript, like Haml for HTML. Allows you to write the same code in a slightly different way:
If and while operators removed extra characters:
And also there is a short form for one line
Simplifies lambda syntax. In CoffeeScript, as well as in Ruby, any function returns the last value.
Allows you to work with arrays as in Python:
And it makes it easier to get a substring or part of an array:
More details can be found on the CoffeeScript website .
The CoffeeScript translator is written in Ruby and distributed through RubyGems:
Using it, you can translate your
In order not to forget to translate the file and not look for a ghostly error, you can specify to automatically broadcast all
By installing Narwhal, you can experiment with CoffeeScript in the interactive console or run coffee scripts outside the browser.
1 - It is in JavaScript 1.6
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 for the 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 begin to push Flash and Silverlight. And recently, JS began to be considered as a server language (for example, node.js ).
That's just in the modern world of syntactic sugar JavaScript code is a bit heavy. With each lambda it’s wrong to write a long string
function() { … }
, there is not enough switch-when and foreach for arrays 1 . After Ruby and Python, even the brackets around the condition in if
or obligatory return
seem like extra characters. That would be the same JavaScript, but with a slightly different syntax. And we have successful examples of syntax changes: Haml and Sassfor HTML and CSS, respectively. They do not hide from the layout (like terrible WebForms), but simply allow you to write the same code with a slightly different syntax, but we constantly monitor the translation result.
CoffeeScript
CoffeeScript for JavaScript, like Haml for HTML. Allows you to write the same code in a slightly different way:
Operators
If and while operators removed extra characters:
while a < 20
a += 1.
And also there is a short form for one line
student.passed(exam.subject) if exam.mark > 2
Lambda
Simplifies lambda syntax. In CoffeeScript, as well as in 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 details can be found on the CoffeeScript website .
Instruments
The 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 look for a ghostly error, you can specify to automatically broadcast all
.coffee
files when changing:coffee -w * .coffee
By installing Narwhal, you can experiment with CoffeeScript in the interactive console or run coffee scripts outside the browser.
Notes
1 - It is in JavaScript 1.6
Array#forEach()
, but it is in jQuery $.each()
, but they work more slowly and the long lambda still destroys them.