Coffeescript 简明教程
CoffeeScript - Overview
目前,JavaScript 是最快的主流动态语言,它被誉为 Web 的通用语。它是由 Brendan Eich 在 1995 年的 10 天内开发的。
At present, JavaScript is the fastest mainstream dynamic language available, and it is known as the lingua franca of the web. It is developed by Brendan Eich in the year of 1995 in 10 days.
由于其有效的功能,JavaScript 变得流行,并迅速风靡全球。它在实验室中存在的时间非常短暂,不足以对该语言进行完善。也许正是出于这个原因,尽管 JavaScript 有好的部分,但它仍然存在大量的设计错误,并且因其古怪而获得了恶名。
Because of its effective features, JavaScript became popular and went global quickly. It was there in lab for a very less time, which was not enough to polish the language. May be for this reason, inspite of its good parts, JavaScript has a bunch of design errors and it bagged a bad reputation of being a quirky language.
What is CoffeeScript ?
CoffeeScript 是一种基于 Ruby 和 Python 的轻量级语言,它可以 transcompiles (从一种源语言编译到另一种) JavaScript。它提供了更好的语法,避免了 JavaScript 中古怪的部分,同时保留了该语言的灵活性和美感。
CoffeeScript is a lightweight language based on Ruby and Python which transcompiles (compiles from one source language to another) into JavaScript. It provides better syntax avoiding the quirky parts of JavaScript, still retaining the flexibility and beauty of the language.
Advantages of CoffeeScript
以下是 CoffeeScript 的优势 −
Following are the advantages of CoffeeScript −
-
Easily understandable − CoffeeScript is a shorthand form of JavaScript, its syntax is pretty simple compared to JavaScript. Using CoffeeScript, we can write clean, clear, and easily understandable codes.
-
Write less do more − For a huge code in JavaScript, we need comparatively very less number of lines of CoffeeScript.
-
Reliable − CoffeeScript is a safe and reliable programming language to write dynamic programs.
-
Readable and maintainable − CoffeeScript provides aliases for most of the operators which makes the code readable. It is also easy to maintain the programs written in CoffeeScript.
-
Class-based inheritance − JavaScript does not have classes. Instead of them, it provides powerful but confusing prototypes. Unlike JavaScript, we can create classes and inherit them in CoffeeScript. In addition to this, it also provides instance and static properties as well as mixins. It uses JavaScript’s native prototype to create classes.
-
No var keyword − There is no need to use the var keyword to create a variable in CoffeeScript, thus we can avoid the accidental or unwanted scope deceleration.
-
Avoids problematic symbols − There is no need to use the problematic semicolons and parenthesis in CoffeeScript. Instead of curly braces, we can use whitespaces to differentiate the block codes like functions, loops, etc.
-
Extensive library support − In CoffeeScript, we can use the libraries of JavaScript and vice versa. Therefore, we have access to a rich set of libraries while working with CoffeeScript.
History of CoffeeScript
-
CoffeeScript is developed by Jeremy Ashkenas. It was first committed in Git On December 13, 2009.
-
Originally the compiler of the CoffeeScript was written in Ruby language.
-
In March 2010, the CoffeeScript compiler was replaced; this time instead of Ruby, they used CoffeeScript itself.
-
And in the same year, CoffeeScript 1.0 was released and at the time of release, it was one of the most wanted projects of the Git hub.
Limitations of CoffeeScript
-
Sensitive to whitespaces − CoffeeScript is very sensitive to whitespaces, so programmers need to be very careful while providing indentations. If we do not maintain proper indentation, the entire code may go wrong.
TutorialsPoint’s CoffeeScript IDE
你可以使用 TutorialsPoint 在我们的编码基础部分提供的 CoffeeScript 编译器来编译 CoffeeScript 文件 https://www.tutorialspoint.com/codingground.htm 。按照以下步骤使用我们的 CoffeeScript 编译器。
You can compile CoffeeScript files using TutorialsPoint’s CoffeeScript compiler provided in our Coding Ground section https://www.tutorialspoint.com/codingground.htm. Follow the steps given below to use our CoffeeScript compiler.
Step 1
点击以下链接,访问我们网站的主页 www.tutorialspoint.com 。
Visit the home page of our website by clicking the following link www.tutorialspoint.com.
Step 2
在主页右上角单击名为 CODING GROUND 的按钮,如下面给出的快照中所示。
Click on the button named CODING GROUND that is located at the top right corner of the homepage as highlighted in the snapshot given below.
Step 3
这将导致我们的 CODING GROUND 部分,该部分为大约 135 种编程语言提供在线终端和 IDE。在在线 IDE 部分中打开 CoffeeScript IDE,这在以下快照中所示。
This will lead to our CODING GROUND section which provides online terminals and IDEs for about 135 programming languages. Open CoffeeScript IDE in the Online IDEs section which is shown in the following snapshot.
Step 4
如果你在 main.coffee 中粘贴你的 CoffeeScript 代码(你可以更改文件名),然后单击 Preview 按钮,那么你可以在控制台中看到已编译的 JavaScript,如下面的快照所示。
If you paste your CoffeeScript code in main.coffee (You can change the file name) and click the Preview button, then you can see the compiled JavaScript in the console as shown in the following snapshot.