Coffeescript 简明教程
CoffeeScript - Functions
一个函数是一个可重复使用代码块,可以在程序的任何地方调用。这消除了重复编写相同代码的需要。它可帮助程序员编写模块化代码。
A function is a block of reusable code that can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes.
函数允许一个程序员将一个大程序划分为多个小而易于管理的函数。
Functions allow a programmer to divide a big program into a number of small and manageable functions.
通常,使用 JavaScript,我们可以定义两种类型的函数 – named functions ,具有函数名称主体的常规函数, Function expressions 。使用函数表达式,我们可以将函数分配给变量。
In general, using JavaScript, we can define two types of functions – named functions, the regular functions with function name body and, Function expressions. Using function expressions, we can assign functions to variables.
//named function
function sayHello(){
return("Hello there");
}
//function expressions
var message = function sayHello(){
return("Hello there");
}
Functions in CoffeeScript
CoffeeScript 中函数的语法与 JavaScript 相比更简单。在 CoffeeScript 中,我们仅定义函数表达式。
The syntax of function in CoffeeScript is simpler as compared to JavaScript. In CoffeeScript, we define only function expressions.
function 关键字在 CoffeeScript 中被取消。若要在此处定义一个函数,我们必须使用一个瘦箭头 ( → )。
The function keyword is eliminated in CoffeeScript. To define a function here, we have to use a thin arrow (→).
在后台,CoffeeScript 编译器将箭头转换为 JavaScript 中的函数定义,如下所示。
Behind the scenes, the CoffeeScript compiler converts the arrow in to the function definition in JavaScript as shown below.
(function() {});
在 CoffeeScript 中使用 return 关键字是非强制性的。CoffeeScript 中的每个函数都会自动返回函数中的最后一条语句。
It is not mandatory to use the return keyword in CoffeeScript. Every function in CoffeeScript returns the last statement in the function automatically.
-
If we want to return to the calling function or return a value before we reach the end of the function, then we can use the return keyword.
-
In addition to in-line functions (functions that are in single line), we can also define multiline functions in CoffeeScript. Since the curly braces are eliminated, we can do it by maintaining proper indentations.
Defining a Function
以下是 CoffeeScript 中定义函数的语法。
Following is the syntax of defining a function in CoffeeScript.
function_name = -> function_body
Example
下面给出了 CoffeeScript 中的一个函数示例。在这里,我们创建了一个名为 greet 的函数。此函数自动返回其中的语句。以 function_example.coffee 的名称将其保存到一个文件中。
Given below is an example of a function in CoffeeScript. In here, we have created a function named greet. This function automatically returns the statement in it. Save it in a file with the name function_example.coffee
greet = -> "This is an example of a function"
通过在命令提示符中执行以下命令对其执行编译。
Compile it by executing the following command in the command prompt.
c:\>coffee -c function_example.coffee
在编译时,它会生成以下 JavaScript 代码。在这里,你可以观察到 CoffeeScript 编译器自动返回名为 greet() 的函数中的字符串值。
On compiling, it generates the following JavaScript code. Here you can observe that the CoffeeScript compiler automatically returned the string value in the function named greet().
// Generated by CoffeeScript 1.10.0
(function() {
var greet;
greet = function() {
return "This is an example of a function";
};
}).call(this);
Multi-line Functions
我们还可以通过维护缩进来定义一个具有多行的函数,而不是大括号。但我们必须与我们在整个函数中一个行的缩进保持一致。
We can also define a function with multiple lines by maintaining indentations instead of curly braces. But we have to be consistent with the indentation we follow for a line throughout a function.
greet = ->
console.log "Hello how are you"
在编译时,以上 CoffeeScript 为你提供了以下 JavaScript 代码。CoffeeScript 编译器获取了我们使用缩进来分隔且置于大括号内的函数主体。
On compiling, the above CoffeeScript gives you the following JavaScript code. The CoffeeScript compiler grabs the body of the function that we have separated using indentations and placed within the curly braces.
// Generated by CoffeeScript 1.10.0
(function() {
var greet;
greet = function() {
return console.log("Hello how are you");
};
}).call(this);
Functions with Arguments
我们还可以使用括号在函数中指定参数,如下所示。
We can also specify arguments in a function using parenthesis as shown below.
add =(a,b) ->
c=a+b
console.log "Sum of the two numbers is: "+c
在编译以上 CoffeeScript 文件时,它会生成以下 JavaScript。
On compiling the above CoffeeScript file, it will generate the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var add;
add = function(a, b) {
var c;
c = a + b;
return console.log("Sum of the two numbers is: " + c);
};
}).call(this);
Invoking a Function
定义一个函数之后,我们需要调用该函数。你只需在函数名后加上圆括号即可调用函数,如以下示例所示。
After defining a function, we need to invoke that function. You can simply invoke a function by placing parenthesis after its name as shown in the following example.
add = ->
a=20;b=30
c=a+b
console.log "Sum of the two numbers is: "+c
add()
在编译时,上述示例会生成以下 JavaScript
On compiling, the above example gives you the following JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var add;
add = function() {
var a, b, c;
a = 20;
b = 30;
c = a + b;
return console.log("Sum of the two numbers is: " + c);
};
add();
}).call(this);
执行上述 CoffeeScript 代码时,会生成以下输出。
On executing the above CoffeeScript code, it generates the following output.
Sum of the two numbers is: 50
Invoking Functions with Arguments
同样地,我们可以通过向函数传递参数来调用它,如下所示。
In the same way, we can invoke a function with arguments by passing them to it as shown below.
my_function argument_1,argument_2
or
my_function (argument_1,argument_2)
Note − 在通过向函数传递参数来调用函数时,使用圆括号是可选的。
Note − While invoking a function by passing arguments to it, the usage of parenthesis is optional.
在以下示例中,我们创建了一个名为 add() 的函数,它接受两个参数,并且我们已经调用了它。
In the following example, we have created a function named add() that accepts two parameters and we have invoked it.
add =(a,b) ->
c=a+b
console.log "Sum of the two numbers is: "+c
add 10,20
在编译时,上述示例会生成以下 JavaScript。
On compiling, the above example gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var add;
add = function(a, b) {
var c;
c = a + b;
return console.log("Sum of the two numbers is: " + c);
};
add(10, 20);
}).call(this);
执行时,上述 CoffeeScript 代码会生成以下输出。
On executing, the above CoffeeScript code it generates the following output.
Sum of the two numbers is: 30
Default Arguments
CoffeeScript 也支持默认参数。我们可以为函数参数分配默认值,如以下示例所示。
CoffeeScript supports default arguments too. We can assign default values to the arguments of a function, as shown in the following example.
add =(a = 1, b = 2) ->
c=a+b
console.log "Sum of the two numbers is: "+c
add 10,20
#Calling the function with default arguments
add()
在编译时,上述 CoffeeScript 会生成以下 JavaScript 文件。
On compiling, the above CoffeeScript generates the following JavaScript file.
// Generated by CoffeeScript 1.10.0
(function() {
var add;
add = function(a, b) {
var c;
if (a == null) {
a = 1;
}
if (b == null) {
b = 2;
}
c = a + b;
return console.log("Sum of the two numbers is: " + c);
};
add(10, 20);
add()
}).call(this);
执行上述 CoffeeScript 代码时,会生成以下输出。
On executing the above CoffeeScript code, it generates the following output.
Sum of the two numbers is: 30
Sum of the two numbers is: 3