Coffeescript 简明教程
CoffeeScript - Syntax
-
* *在上一个章节中,我们已经看到了如何安装CoffeeScript。在本章中,让我们了解CoffeeScript的语法。
In the previous chapter, we have seen how to install CoffeeScript. In this chapter, let us check out the syntax of CoffeeScript.
-
* *与JavaScript的语法相比,CoffeeScript的语法更优雅。它避免了像花括号、分号和变量减速这样的麻烦特性。
The syntax of CoffeeScript is more graceful when compared to the syntax of JavaScript. It avoids the troublesome features like curly braces, semicolons, and variable decelerations.
CoffeeScript Statements
-
* *与C、C++和Java等许多其他编程语言不同,CoffeeScript中的语句并不以分号 (;) 结尾。相反,CoffeeScript编译器将每一行都视为一个单独的语句。
Unlike many other programming languages like C, C++, and Java, the statements in CoffeeScript do not end with semicolons (;). Instead of that, every new line is considered as a separate statement by the CoffeeScript compiler.
CoffeeScript Variables (No var Keyword)
-
* 在JavaScript中,我们使用 *var 关键字在创建变量之前声明变量,如下所示。
In JavaScript, we declare a variable using the var keyword before creating it, as shown below.
var name = "Javed"
var age = 20
-
* 在CoffeeScript中创建变量时,不需要使用 *var 关键字来声明变量。我们只需通过给它赋值来直接创建变量,如下所示。
While creating variables in CoffeeScript, there is no need to declare them using the var keyword. We can directly create a variable just by assigning a value to it as shown below.
name = "Javed"
age = 20
No Parentheses
-
* 通常,我们在声明函数、调用函数以及分隔代码块时使用括号以避免歧义。在CoffeeScript中,不需要使用括号,而在创建函数时,我们使用箭头标记 ( *→ ) 代替括号,如下所示。
In general, we use parenthesis while declaring the function, calling it, and also to separate the code blocks to avoid ambiguity. In CoffeeScript, there is no need to use parentheses, and while creating functions, we use an arrow mark (→) instead of parentheses as shown below.
myfunction = -> alert "Hello"
-
* *不过,在某些情况下,我们仍然必须使用括号。在调用没有参数的函数时,我们将使用括号。例如,如果我们在CoffeeScript中有一个名为my_function的函数,那么我们必须按照如下所示调用它。
Still, we have to use parentheses in certain scenarios. While calling functions without parameters, we will use parentheses. For example, if we have a function named my_function in CoffeeScript, then we have to call it as shown below.
my_function()
-
* *以同样的方式,我们也可以使用括号来分隔模棱两可的代码。如果你观察以下示例,没有大括号,结果是2233,有括号,结果是45。
In the same way, we can also separate the ambiguous code using parentheses. If you observe the following example, without braces, the result is 2233 and with braces, it will be 45.
alert "The result is "+(22+33)
No Curly Braces
-
* *在JavaScript中,对于诸如函数、循环和条件语句之类的块代码,我们使用大括号。在CoffeeScript中,不需要使用大括号。相反,我们必须在正文中保持适当的缩进(空格)。这是受Python语言启发的特性。
In JavaScript, for the block codes such as functions, loops, and conditional statements, we use curly braces. In CoffeeScript, there is no need to use curly braces. Instead, we have to maintain proper indentations (white spaces) within the body. This is the feature which is inspired from the Python language.
-
* *以下是一个CoffeeScript函数的示例。在这里你可以观察到,我们使用了三个空格作为缩进来分隔函数的主体,而不是大括号。
Following is an example of a function in CoffeeScript. Here you can observe that instead of curly braces, we have used three whitespaces as indentation to separate the body of the function.
myfunction = ->
name = "John"
alert "Hello"+name
CoffeeScript Comments
-
* *在任何编程语言中,我们使用注释来编写我们编写的代码的描述。这些注释不被认为是程序的一部分。CoffeeScript中的注释类似于Ruby语言的注释。CoffeeScript提供了两种类型的注释,如下所述 −
In any programming language, we use comments to write description about the code we have written. These comments are not considered as the part of the programs. The comments in CoffeeScript are similar to the comments of Ruby language. CoffeeScript provides two types of comments as follows −
Single-line Comments
当我们希望在 CoffeeScript 中注释某一行时,我们只需要在它之前放置一个井号标记,如下所示。
Whenever we want to comment a single line in CoffeeScript, we just need to place a hash tag before it as shown below.
# This is the single line comment in CoffeeScript
在 CoffeeScript 编译器中,每个单行标记后 ( # ) 的行都被视为注释,它将编译给定文件中未注释的其余代码。
Every single line that follows a hash tag (#) is considered as a comment by the CoffeeScript compiler and it compiles the rest of the code in the given file except the comments.
Multiline Comments
当我们希望在 CoffeeScript 中注释多行(多行)时,我们可以通过将这些行包裹在对三重井号标记中来实现,如下所示。
Whenever we want to comment more than one line in CoffeeScript (multiple lines), we can do that by wrapping those lines within a pair of triple hash tags as shown below.
###
These are the multi line comments in CoffeeScript
We can write as many number of lines as we want
within the pair of triple hash tags.
###
CoffeeScript Reserved keywords
CoffeeScript 中所有保留字的列表在下表中给出。它们不能用作 CoffeeScript 变量、函数、方法、循环标签或任何对象名称。
A list of all the reserved words in CoffeeScript are given in the following table. They cannot be used as CoffeeScript variables, functions, methods, loop labels, or any object names.
case default function var void with const let enum export import native _hasProp _extends _slice _bind __indexOf implements |
else interface package private protected public static yield true false null this new delete typeof in arguments eval |
instanceof return throw break continue debugger if else switch for while do try catch finally class extends super |
undefined then unless until loop of by when and or is isnt not yes no on off |