CoffeeScript - 功能

函数是一个可重用代码块,可以在程序中的任何位置调用.这消除了一次又一次地编写相同代码的需要.它帮助程序员编写模块化代码.

函数允许程序员将大程序划分为许多小而易于管理的函数.

一般情况下使用JavaScript,我们可以定义两种类型的函数 -  命名函数,具有函数名称body的常规函数和函数表达式.使用函数表达式,我们可以为变量赋值.

//named function
function sayHello(){
   return("Hello there");
}
 
//function expressions
var message = function sayHello(){
   return("Hello there");
}

CoffeeScript中的函数

与JavaScript相比,CoffeeScript中的函数语法更简单.在CoffeeScript中,我们只定义函数表达式.

在CoffeeScript中消除了函数关键字.要在此定义函数,我们必须使用细箭头(  - > ).

在幕后,CoffeeScript编译器将箭头转换为JavaScript中的函数定义如下所示.

 
(function(){});

在CoffeeScript中使用 return 关键字不是强制性的. CoffeeScript中的每个函数都会自动返回函数中的最后一个语句.

  • 如果我们想返回调用函数或返回在我们到达函数结尾之前的值,然后我们可以使用 return 关键字.

  • 除了内联函数(单行函数),我们也可以在CoffeeScript中定义多行函数.由于花括号被删除,我们可以通过保持适当的缩进来实现.

定义函数

以下是在CoffeeScript中定义函数的语法.

function_name = -> function_body

示例

以下是CoffeeScript中函数的示例.在这里,我们创建了一个名为 greet 的函数.此函数自动返回其中的语句.将其保存在名为 function_example.coffee

的文件中.

greet = -> "This is an example of a function"

通过在命令提示符中执行以下命令来编译它.

 
c:\> coffee -c function_example.coffee

在编译时,它会生成以下JavaScript代码.在这里,您可以观察到CoffeeScript编译器自动在名为 greet()的函数中返回字符串值.

// Generated by CoffeeScript 1.10.0
(function() {
  var greet;
  
  greet = function() {
    return "This is an example of a function";
  };

}).call(this);

多行函数

我们还可以通过维护缩进而不是花括号来定义具有多行的函数.但是我们必须与整个函数中一行所遵循的缩进一致.

greet =  ->
  console.log "Hello how are you"

在编译时,上面的CoffeeScript为您提供了以下JavaScript代码. CoffeeScript编译器抓取我们使用缩进分隔并放在花括号内的函数体.

// Generated by CoffeeScript 1.10.0
(function() {
  var greet;

  greet = function() {
    return console.log("Hello how are you");
  };

}).call(this);

带参数的函数

我们也可以使用括号在函数中指定参数,如下所示.

add =(a,b) ->
  c=a+b
  console.log "Sum of the two numbers is: "+c

编译上述CoffeeScript文件,它将生成以下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);

调用函数

定义函数后,我们需要调用该函数.您可以通过在其名称后面放置括号来调用函数,如以下示例所示.

add = ->
  a=20;b=30
  c=a+b
  console.log "Sum of the two numbers is: "+c  
add()

在编译时,上面的示例为您提供以下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代码时,它会生成以下输出.

Sum of the two numbers is: 50

调用带参数的函数

以同样的方式,我们可以通过将参数传递给它来调用函数,如下所示.

 
 my_function argument_1,argument_2 
或
 my_function( argument_1,argument_2)

注意 : 在通过向其传递参数来调用函数时,括号的使用是可选的.

在下面的示例中,我们创建了一个名为 add()的函数,接受两个参数,我们已经调用它.

add =(a,b) ->
  c=a+b
  console.log "Sum of the two numbers is: "+c
add 10,20

在编译时,上面的例子给出了以下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代码会生成以下输出.

Sum of the two numbers is: 30

默认参数

CoffeeScript也支持默认参数.我们可以为函数的参数赋值默认值,如下例所示.

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文件.

// 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代码时,它会生成以下输出.

Sum of the two numbers is: 30
Sum of the two numbers is: 3