Coffeescript 简明教程
CoffeeScript - Command-line utility
在 Node.js 上安装 CoffeeScript 后,我们可以访问 coffee-command line utility 。在此, coffee 命令是关键命令。使用此命令的各种选项,我们可以编译并执行 CoffeeScript 文件。
On installing CoffeeScript on Node.js, we can access the coffee-command line utility. In here, the coffee command is the key command. Using various options of this command, we can compile and execute the CoffeeScript files.
您可以使用 coffee 命令的 -h 或 --help 选项查看 coffee 命令的选项列表。打开 Node.js command prompt 并在此中执行以下命令。
You can see the list of options of the coffee command using its -h or --help option. Open the Node.js command prompt and execute the following command in it.
c:\>coffee -help
此命令会给您 coffee 的各种选项列表,以及其中每个选项执行的操作的说明,如下所示。
This command gives you the list of various options of the coffee, along with the description of the operation performed by each of them as shown below.
Compiling the CoffeeScript Code
CoffeeScript 文件以扩展名为 .coffee 保存。您可以使用 coffee 命令的 -c or --compile 选项来编译这些文件,如下所示:
The CoffeeScript files are saved with the extension .coffee. You can compile these files using the -c or --compile option of the coffee command as shown below.
c:\>coffee -c filename.coffee
Example
假设您的系统中有一个包含以下 CoffeeScript 代码的文件,该代码会在控制台上打印一条消息。
Suppose there is a file in your system with the following CoffeeScript code which prints a message on the console.
name = "Raju"
console.log "Hello"+name+" Welcome to Tutorialspoint"
Note − console.log() 函数在控制台上打印给定的字符串。
Note − The console.log() function prints the given string on the console.
为编译以上代码,请将其保存在名为 sample.coffee 的文件中。打开 Node.js 命令提示符。浏览到您已保存文件的位置,并使用 coffee command-line utility 的 coffee 命令的 -c 选项进行编译,如下所示:
To compile the above code, save it in a file with the name sample.coffee. Open the Node.js command prompt. Browse through the path where you have saved the file and compile it using the -c option of the coffee command of the coffee command-line utility as shown below.
c:\> coffee -c sample.coffee
执行上述命令后,CoffeeScript 编译器会编译给定的文件 (sample.coffee),并将其保存在当前位置,名称为 sample.js,如下所示。
On executing the above command, the CoffeeScript compiler compiles the given file (sample.coffee) and saves it in the current location with a name sample.js as shown below.
若你打开 sample.js 文件,便可观察到生成的 JavaScript,如下所示。
If you open the sample.js file, you can observe the generated JavaScript as shown below.
// Generated by CoffeeScript 1.10.0
(function() {
var name;
name = "Raju";
console.log("Hello " + name + " Welcome to Tutorialspoint");
}).call(this);
Executing the CoffeeScript code
你可以通过在 Node.js 命令提示符中将文件名传递给 coffee 命令,轻松执行 CoffeeScript 文件,如下所示。
You can execute a CoffeeScript file by simply passing the file name to the coffee command in the Node.js command prompt as follows.
c:\> coffee sample.coffee
Example
例如,让我们执行 sample.coffee 文件。为此,打开 Node.js 命令提示符。浏览已保存该文件的路径,并通过将文件名称直接传递给 coffee 命令来执行该文件,如下所示。
For example, let us execute the sample.coffee file. For this, open the Node.js command prompt. Browse through the path where you have saved the file and execute the file by directly passing its name to the coffee command as shown below.
Watch and Compile
在某些情况下,我们可能会对脚本进行大量的更改。使用 coffee 命令的 –w 选项,你可以监视你的脚本以进行更改。
In some scenarios, there is a chance that we do a lot of changes to our scripts. Using the –w option of the coffee command, you watch your scripts for changes.
你可以使用 -wc 选项同时监视和编译文件,如下所示。当我们使用此选项时,每次你在脚本中进行更改时,文件都会重新编译。
You can watch and compile a file simultaneously using the -wc option as shown below. When we use this option, the file will be recompiled each time you make changes in your script.
c:\>coffee -wc file_name
Example
假设我们已使用 -wc 选项编译了一个名为 sample.coffee 的文件,并且我们修改了三次脚本。每次更改脚本时, .coffee 文件都会重新编译,并留下 Node.js 命令提示符,如下所示。
Suppose we have compiled a file named sample.coffee using the -wc option and we modified the script thrice. Each time we change the script, the .coffee file is recompiled leaving the Node.js command prompt as shown below.
Setting the Output Directory
使用 -o 选项,我们可以设置输出目录,以便放置编译后的 JavaScript 文件,如下所示。
Using the -o option, we can set the output directory to place the compiled JavaScript files as shown below.
c:\>coffee -o "Required path where we want our .js files" file_name
Example
让我们将 sample.coffee 文件的 JavaScript 代码保存在 E 驱动器中名为 data 的文件夹中,使用 -o 选项,在命令提示符中执行以下命令。
Let us save the JavaScript code of the sample.coffee file in a folder named data in the E drive using the -o option by executing the following command in the command prompt.
c:\>coffee -o E://data sample.coffee
执行上述命令后,以下是给定文件夹的快照。在这里你可以观察到 sample.coffee 的 JavaScript 文件
Following is the snapshot of the given folder after executing the above command. Here you can observe the JavaScript file of the sample.coffee
Print the Compiled JavaScript
如果我们想将编译后的 javascript 打印在控制台上,我们必须使用 coffee 命令的 -p 选项,如下所示。
If we want to print the compiled javascript on the console itself, we have to use the -p option of the coffee command as shown below.
c:\>coffee -p file_name
The REPL (Read Evaluate Print Loop)
CoffeeScript 为你提供了 REPL 交互式 shell。此 shell 用于评估 CoffeeScript 表达式。你可以在此 shell 中键入任何 CoffeeScript 代码并立即获得结果。你可以通过不带任何选项执行 coffee 命令来打开 REPL,如下所示。
CoffeeScript provides you an REPL-interactive shell. This shell is used to evaluate the CoffeeScript expressions. You can type any CoffeeScript code in this shell and get the result immediately. You can open REPL by executing the coffee command without any options as shown below.
使用此 shell,我们可以向变量分配值,创建函数和评估结果。如下面的屏幕截图所示,如果我们在 REPL 中调用函数,它将打印函数的值。如果我们向它提供一个表达式,它会评估并打印表达式的结果。并且如果我们只是在其中键入语句,它将打印最后一条语句的值。
Using this shell, we can assign values to variables, create functions, and evaluate results. As shown in the following screenshot, if we call functions in REPL, it prints the value of the function. If we give an expression to it, it evaluates and prints the result of the expression. And if we simply type the statements in it, it prints the value of the last statement.
在 REPL 中,你可以通过按 ctrl+v 访问多行模式,在多行模式中,你可以使用多行 (如函数) 来评估代码,并且你可以通过再次按 ctrl+v 从中返回到 REPL 模式。下面是多行模式使用的一个示例。
In REPL, you can access multiple line mode by pressing ctrl+v where you can evaluate the code with multiple lines (like functions) and you can get back to REPL mode from it by pressing ctrl+v again. Here is an example usage of the multi line mode.
Running CoffeeScript through Browser
我们可以像 JavaScript 一样使用 HTML 的 <script> 标签运行 CoffeeScript,如下所示。
We can run CoffeeScript using the <script> tag of the HTML just like JavaScript as shown below.
<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>
但为此,我们必须在每个应用程序中导入该库,并且在显示输出之前,CoffeeScript 代码会逐行解释。这将减慢你的应用程序的速度,因此不建议使用此方法。
But for this, we have to import the library in each application and the CoffeeScript code will be interpreted line by line before the output is shown. This will slow down your applications, therefore this approach is not recommended.
因此,要在应用程序中使用 CoffeeScript,你需要使用 Coffee 命令行实用程序对它们进行预编译,然后你可以在应用程序中使用生成的 JavaScript。
Therefore, to use CoffeeScript in your applications, you need to pre-compile them using the Coffee command-line utility and then you can use the generated JavaScript in your applications.