CoffeeScript教程

CoffeeScript 函数

函数是一块可重用的代码,可以在程序的任何地方调用。这消除了一次又一次编写相同代码的需要。它可以帮助程序员编写模块化代码。
函数允许程序员将一个大程序划分为许多小而易于管理的函数。
一般来说,使用 JavaScript,我们可以定义两种类型的函数- 命名函数,带有函数名主体的常规函数​​和 函数表达式。使用函数表达式,我们可以将函数赋值给变量。
//named function
function sayHello(){
   return("Hello there");
}
 
//function expressions
var message = function sayHello(){
   return("Hello there");
}

CoffeeScript 中的函数

CoffeeScript 中的函数语法比 JavaScript 更简单。在 CoffeeScript 中,我们只定义函数表达式。
function 关键字在 CoffeeScript 中被消除了。要在此处定义函数,我们必须使用细箭头 ( ->)。
在幕后,CoffeeScript 编译器将箭头 in 转换为 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
or
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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4