Experience in the use of Coffeescript

Source: Internet
Author: User

Today, more and more secondary languages replace the traditional web three languages, respectively, jade/hbs corresponding html,sass/less corresponding to css,typescript/coffeescript corresponding JavaScript, of course, In fact, can be more than listed above, listed above should be a bit of penetration of the secondary language. In this, I think the most perfect should be sass, this article does not discuss it, only discuss coffeescript.
But all of the challenges of tradition, among the ranks and not die of new things, should have its advantages, Coffeescript Also, this language is aimed at the bloated JavaScript, after all, when the advent of JavaScript did not expect to have today's results, However, JavaScript in Toibe ranked to unprecedented heights, the current JavaScript development into the Web field of the most urgent technical personnel. Coffeescript is also a focus for JavaScript and into everyone's vision. The following chat with the Cofffeescript to do a few projects after the real experience, just experience.


one, concise, too concise, resulting in copy paste into the IDE format is always messy


But after reading the Coffeescript official website of those few short examples, everyone wants to try, I also so, did not see the children's shoes can click the following link into the official website
http://coffeescript.org/

If you can't open it, refresh it a few times, or you won't open it, then you'll cross that barrier and the world is right here, you know.

# Assignment:
Number = 42
opposite = True

# Conditions:
Number = -42 if opposite

# functions:
Square = (x)-> x * x

# Arrays:
list = [1, 2, 3, 4, 5]

# Objects:
Math =
Root:Math.sqrt
Square:square
Cube: (x)-> x * square x

# Splats:
Race = (Winner, runners ...)->
Print winner, runners

# existence:
Alert "I knew it!" if Elvis?

# Array Comprehensions:
cubes = (math.cube num for num in list)

Post-compile example

var cubes, list, math, num, number, opposite, race, Square,
Slice = [].slice;

Number = 42;

opposite = true;

if (opposite) {
Number =-42;
}

Square = function (x) {
return x * x;
};

list = [1, 2, 3, 4, 5];

Math = {
ROOT:MATH.SQRT,
Square:square,
Cube:function (x) {
return x * Square (x);
}
};

Race = function () {
var runners, winner;
Winner = Arguments[0], runners = 2 <= arguments.length? Slice.call (arguments, 1): [];
Return print (winner, runners);
};

if (typeof Elvis!== "undefined" && Elvis!== null) {
Alert ("I knew it!");
}

Cubes = (function () {
var i, Len, results;
results = [];
for (i = 0, len = list.length i < len; i++) {
num = List[i];
Results.push (Math.cube (num));
}
return results;
})();

Is it a little exciting? It's simple, simple, and the premise is to sacrifice a bunch of terminator, if without curly braces, function without curly braces, so you need to be particularly careful when you copy large pieces of code, right? I use Webstorm, the so-called most awesome front-end editor in the copying of large pieces of Coffeescript code, there will still be inaccurate indentation problem, because it does not blame Webstorm, but really bad judgment
Original code:


A = (NUM1, num2)->
NUM1 + num2

You copy the code in the back.


NUM1 * num2

There are two kinds of results, either


A = (NUM1, num2)->
NUM1 + num2
NUM1 * num2

Or is


A = (NUM1, num2)->
NUM1 + num2
NUM1 * num2

So if the IDE helps you indent by default, instead of the one you want, you have to select the code you just copied and then tab again, or the default does not shrink and then you want the second, so you select the copied Code again tab

Second, the Ecological circle

Sometimes, to find some solution code, most of the online paste of the various solution code is JavaScript format, such as you want to search some plug-in examples of the use of the Code, the results found that people posted in javascript format, So you have to follow the example code written by others to change the coffeescript format, this is very time-consuming. The purpose of using coffeescript is to be concise, to reduce coding time, but the result is counterproductive, all external source code you have to convert. As of now, I have not found any of the Coffeescript code directly attached. In fact, you think about it, if you go to the StackOverflow on the code, you will be coffeescript and typescript format of all paste it? What about the other languages I didn't list? What to do?

Third, the previously written code can not be reused

If you have a lot of JavaScript project experience, then all the JavaScript code you have to reuse to your Coffeescript project, you need to translate it, this process has no practical significance, but also a waste of time, think about all fear.

Quad, IDE support is not good

I use the webstorm, this editor to write JavaScript, not to say, is absolutely an artifact, but when you use it to write Coffeescript, you will find many places unsatisfactory. So I think even JavaScript is bloated, but with these awesome ides it can be very handy to encode. For example, there is a function I do not dare to use when writing coffeescript, that is, automatically formatted code, Coffeescript is bounded by the indentation, in case of the IDE silly lack of indentation in the mess that your program is basically destroyed. JavaScript has delimiters for curly braces, so don't worry that code formatting can cause the program to be corrupted.

At last

As I said before, the Sass/less is the most successful, why are they better than coffeescript? Because they are completely compatible with native CSS syntax formatting, this is especially important, and then you can add variables/methods to the above. In other words, you know that Jade will also have a similar coffeescript situation, of course, Jade is much better than coffeescript, because the code for writing HTML in the project is still very small, even if the replication will not take much longer. Ready to use Coffeescript to do the project's children's shoes, be careful ah!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.