目录

CoffeeScript - 功能( Functions)

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

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

通常,使用JavaScript,我们可以定义两种类型的函数 - named functions ,带函数名称的常规函数​​和Function expressions 。 使用函数表达式,我们可以为变量赋值。

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

CoffeeScript中的函数

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

CoffeeScript中删除了function关键字。 要在此定义函数,我们必须使用细箭头( -》 )。

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

(function() {});

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

  • 如果我们想要返回到调用函数或在函数结束之前返回一个值,那么我们可以使用return关键字。

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

定义一个函数 (Defining a Function)

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

function_name = <b class="notranslate">-></b> function_body

例子 (Example)

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

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

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

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);

Multi-line 函数

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

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)

Note - 在通过向其传递参数来调用函数时,括号的用法是可选的。

在下面的示例中,我们创建了一个名为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
↑回到顶部↑
WIKI教程 @2018