Initial understanding of coffeescript and the benefits it brings to developers

Source: Internet
Author: User
Keywords Advantages developers Coffeescript

Coffeescript can be compiled into efficient JavaScript, in addition to running JavaScript in a Web browser, you can combine it with technologies such as Node.js to build server applications. This series of articles consists of 4 parts, and part 1th will provide a preliminary understanding of coffeescript and the unique advantages it brings to developers. You can build the Coffeescript compiler and use it to create code that is ready to run in a browser or service.

The Coffeescript programming language is built on top of JavaScript and can be compiled into efficient JavaScript that you can run on a Web browser or combine with technologies such as Node.js for building server-side applications. The compilation process is usually simple, and the generated JavaScript is consistent with many best practices. In this article, we will learn about the features of the Coffeescript programming language. After you install and run the Coffeescript compiler, you will be presented with a simple example of using Coffeescript in a Web page.

Coffeescript Attraction

JavaScript is now the most important programming language. This is a browser language that is appearing more and more frequently in desktops and mobile applications. With the increasing popularity of node.js, JavaScript has become a viable option for servers and system applications. Some developers strongly resist JavaScript because of their inconsistent syntax and quirky implementations. However, as JavaScript virtual machines become more standardized, the odd implementations are gradually diminishing. Inconsistent syntax may be solved with JavaScript's next revolution: the advent of Ecmascript.next. Ecmascript.next is an emerging standard, deeply influenced by Coffeescript. However, JavaScript syntax needs to be further improved before the new standards are accepted and are implemented by a popular virtual machine.

If you are waiting for JavaScript to run, Coffeescript will be an attractive choice. From a grammatical point of view, JavaScript is a hodgepodge of choices, and it has the function of many functional programming languages, especially by Scheme. However, Scheme is a very simple syntax built on the S-expression. JavaScript follows many concepts in Scheme, but it does not use its syntax. JavaScript, on the other hand, has syntax similar to C. The result is a language with a functional concept, but with a lengthy syntax and no natural construct to express these concepts, for example, JavaScript allows higher-order functions, such as those whose input parameters contain other functions. This is both functional and powerful, and is a feature that many languages lack. However, JavaScript syntax is less elegant, as shown in Listing 1.

Listing 1. Ugly JavaScript

pmb.requestpaymentinfo (' type ', function (info) {$ (' result '). InnerHTML = Info.name;});

The example contains a number of boilerplate code, such as parentheses, commas, braces, semicolons, and some language keywords that are not really needed.

The primary use of JavaScript is as a client-side WEB application language. Desktop and mobile application frameworks such as Cocoa, Windows®, Forms, and Android are object-oriented. Object-oriented paradigms are not perfect, but they are ideal for applications that use graphical user interfaces. JavaScript is also an object-oriented language with inheritance, but it is a prototype inheritance and not a class based language that is used by most application frameworks. Therefore, using JavaScript for application programming can be tedious.

Coffeescript solves the pain points of JavaScript. Coffeescript:

provides a simpler syntax that reduces boilerplate code, such as parentheses and commas, using spaces as a way of organizing blocks of code to provide a simple syntax with expression functions to provide class based inheritance (optional, but useful for application development)

You might think that there are certain disadvantages compared to javascript,coffeescript, because its syntax is more abstract. For example, will coffeescript be slower than JavaScript? Would you need a larger Run-time library? In fact, Coffeescript will be compiled into concise, efficient JavaScript, and you'll always see what's being compiled, so you can be confident that you don't introduce too much stuff. Also, because Coffeescript will be fully compiled into functional JavaScript, no run-time libraries of any type are required. The syntax provided by Coffeescript allows you to take full advantage of the power of JavaScript, with only a small run-time overhead.

For

As mentioned above, you can use Coffeescript to write the server and system applications running on Node.js, but there is a deeper relationship between Coffeescript and Node.js. To install Coffeescript, you need to install Node.js first because:

Coffeescript uses the node's package Manager NPM to distribute as a package for node.js. Coffescript must be compiled, and its compilers are actually written using Coffeescript, so a JavaScript runtime is required to complete its compilation. The V8 JavaScript virtual machine as the Node.js core is ideal for this task.

To follow the example in this article, you need to install Node.js first.

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.