📜 ⬆️ ⬇️

Translation of the book "The Little Book on CoffeeScript"


Greetings, habrayuzer!
I recently decided to study CoffeeScript. Looking for a book on this language, I stumbled upon The Little Book on CoffeeScript . However, this book is in English - and I thought, why not translate it into Russian? A small part of the translation has already been completed, and I would be glad if the community supported the initiative and helped with the translation (I myself am not so an expert in JS, so the translation is crooked in places, this needs to be corrected).

You can follow the translation process here . Below I attach the translation of the first chapter.

The Little Book on CoffeeScript. Chapter 1. Introduction


What is CoffeeScript?


CoffeeScript is a small programming language that can be compiled into JavaScript. Ruby and Python influenced him, so many features of these languages ​​are implemented in it. This book will allow you to learn CoffeeScript, understand the good practices of this language and start creating client applications with it. Since CoffeeScript is a rather small language, this book is also small - there are only 5 chapters in it.
')
This material is a free translation of The Little Book on CoffeeScript, written by Alex McCaw ( macman ) with the help of David Griffiths, Satoshi Murakami, and Jeremy Ashkenaz . Translator - Andrei Romanov .

If you notice a typo, inaccuracy in the translation, or you just want to offer something about this book, feel free to write to me .

So let's get started. Why is CoffeeScript better than pure javascript? Firstly, because we have to write less code - CoffeeScript is very laconic. My experience is that CoffeeScript code takes between a third and a half of the original JavaScript code. In addition, CoffeeScript has some interesting features, such as: array constructors, `prototype`-aliases, and classes. All this in aggregate also reduces the amount of code that has to be dialed.

Moreover, JavaScript has many pitfalls that can confuse an inexperienced developer. CoffeeScript neatly bypasses these oddities of the language, leaving the developer only a set of excellent working functions without any difficulties and pitfalls.

CoffeeScript is not a subset of JavaScript. Although you can access external libraries in CoffeeScript code, you will get a syntax error if you try to compile pure JavaScript code. It should also be noted that CoffeeScript is compiled into pure JavaScript, and not interpreted (as, for example, Python).

To begin to dispel some common misconceptions. You need to know JavaScript to write CoffeeScript code, since, for example, analyzing errors when executing code requires JS knowledge. However, I will make a reservation that in most cases errors are quite obvious. Another CoffeeScript myth is that supposedly compiled JavaScript code will run slower than its counterpart written by a programmer. In practice, it is clear that this is just a myth - in some cases, compiled CoffeeScript works even faster.

What are the disadvantages of using CoffeeScript? First, it requires compilation. However, this problem is mitigated by the fact that CoffeeScript is trying to produce clean and readable JS code. Also, this problem is not very significant due to server solutions that automate the compilation. Secondly, at the moment the community is not very large, and it will be relatively difficult for you to find developers who work with CoffeeScript. However, CoffeeScript is quickly gaining popularity. It is worth mentioning that now in the CoffeeScript IRC chat you can quickly get an answer to almost any question of interest.

CoffeeScript is not browser bound, and can also be used on the server side — for example, in conjunction with Node.js. By the way, CoffeeScript is widely known due to the fact that it is used by default in Ruby on Rails (starting from version 3.1).

Perhaps enough words have already been said for you to give a rough idea of ​​what CoffeeScript is. It's time to dive into his study! Remember that the time spent on studying now will pay off many times later.

Initial installation and configuration


One of the easiest ways to get acquainted with CoffeeScript is to use it right in the browser. Go to http://coffeescript.org and click on the menu item Try CoffeeScript . This site uses the browser version of the CoffeeScript compiler. It converts any CoffeeScript code from the left half of the screen into JavaScript code displayed in the right half of the screen.

You can also convert JavaScript to CoffeeScript using the js2coffee project. This service is especially useful when migrating JavaScript projects to CoffeeScript.

If the need arises, you can use the CoffeeScript browser compiler yourself. To do this, you will need to connect this script to the page and correctly identify the script tag that will contain the CoffeeScript code.

<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js" type="text/javascript" charset="utf-8"></script> <script type="text/coffeescript"> # Some CoffeeScript </script> 

Obviously, in production you hardly want to use this compilation option, since it is slow. So you should take a look at the Node.js compiler. To install it, make sure you have a working latest version of Node.js and npm (package manager for Node). You can install CoffeeScript with the following command:

 npm install -g coffee-script 

After installation, the coffee command will be available to you. If you execute it without arguments, you will get access to the CoffeeScript console, in which you can execute any CoffeeScript code. To compile any files, use the `coffee` command with the argument --compile or its alias, -c .

 coffee --compile my-script.coffee 

If you do not specify the --output argument, CoffeeScript will compile the code into a js file with the same name as the .coffee file. In this case, we will get the compiled file my-script.js. Note that when compiling, CoffeeScript will overwrite all files if they already exist. To see the list of available arguments, run the coffee command with the argument --help or the alias -h.

As you can see above, .coffee is the standard extension of CoffeeScript files. It also allows text editors like TextMate or Sublime Text to determine the language used in the file. Thanks to this, the editor will automatically turn on the necessary syntax highlighting. By default, TextMate and Sublime Text do not support CoffeeScript, but you can easily install a plugin ( for TextMate , for Sublime Text ) that adds support for it.

Probably, all these troubles with compilation tire you a little. However, do not despair - we will take the path to solve this problem, but first, let's look at the syntax of the language.

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


All Articles