CoffeeScript教程

CoffeeScript 推导式

在上一章中,我们学习了 CoffeeScript、 while 及其变体提供的各种循环。除此之外,CoffeeScript 还提供了额外的循环结构,称为 推导式
如果我们显式添加可选的保护子句和当前数组索引的值,这些推导式将替换其他编程语言中的 for 循环。使用推导式,我们可以迭代数组和对象,迭代数组的推导式是表达式,我们可以在函数中返回它们或赋值给变量。
声明和说明
for..in comprehensions
for..in 推导式是 CoffeeScript 中推导式的基本形式,使用我们可以迭代列表或数组的元素。
for..of comprehensions
就像数组一样,CoffeeScriptScript 提供了一个容器来存储称为对象的键值对。我们可以使用 CoffeeScript 提供的 for..of 推导式迭代对象。
列表推导
CoffeeScript 中的 列表推导用于映射数组对象到另一个数组。

推导式指数

元素列表/数组有一个索引,可用于推导。您可以使用如下所示的变量在推导式中使用它。
for student,i in [element1, element2, element3]

示例

以下示例演示了 CoffeeScript 中 for...in 推导式索引的用法。将此代码保存在名为 for_in_index.coffee的文件中
for student,i in ['Ram', 'Mohammed', 'John']
   console.log "The name of the student with id "+i+" is: "+student 
打开 命令提示符并编译.coffee文件,如下所示。
c:\> coffee-c for_in_index.coffee
在编译时,它会为您提供以下 JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
  var i, j, len, ref, student;
  ref = ['Ram', 'Mohammed', 'John'];
  for (i = j = 0, len = ref.length; j < len; i = ++j) {
    student = ref[i];
    console.log("The name of the student with id " + i + " is: " + student);
  }
}).call(this);
现在,再次打开 命令提示符并运行CoffeeScript文件,如下所示。
c:\> coffee for_in_index.coffee
执行时,CoffeeScript 文件产生以下输出。
The name of the student with id 0 is: Ram
The name of the student with id 1 is: Mohammed
The name of the student with id 2 is: John 

后缀形式的推导

就像后缀 ifunless 一样,CoffeeScript 提供了 Comprehensions 的后缀形式,这在编写代码时很方便。使用它,我们可以在一行中编写 for..in 推导式,如下所示。
#Postfix for..in comprehension
console.log student for student in ['Ram', 'Mohammed', 'John']
#postfix for..of comprehension
console.log key+"::"+value for key,value of { name: "Mohammed", age: 24, phone: 9848022338}
显示示例

赋值给变量

我们用来迭代数组的推导式可以赋值给一个变量,也可以由函数返回。

示例

考虑下面给出的示例。在这里您可以观察到我们使用 for..in 推导式检索了数组的元素,并将其分配给名为 names 的变量。我们还有一个函数,它使用 return 关键字显式返回一个推导式。将此代码保存在名为 example.coffee的文件中
my_function =->
   student = ['Ram', 'Mohammed', 'John']
   
   #Assigning comprehension to a variable
   names = (x for x in student )
   console.log "The contents of the variable names are ::"+names
   
   #Returning the comprehension
   return x for x in student
console.log "The value returned by the function is "+my_function() 
打开 命令提示符并编译.coffee文件,如下所示。
c:\> coffee-c example.coffee
在编译时,它会为您提供以下 JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
  var my_function;
  my_function = function() {
    var i, len, names, student, x;
    student = ['Ram', 'Mohammed', 'John'];
    names = (function() {
      var i, len, results;
      results = [];
      for (i = 0, len = student.length; i < len; i++) {
        x = student[i];
        results.push(x);
      }
      return results;
    })();
    console.log("The contents of the variable names are ::" + names);
    for (i = 0, len = student.length; i < len; i++) {
      x = student[i];
      return x;
    }
  };
  console.log("The value returned by the function is " + my_function());
}).call(this);
现在,再次打开 命令提示符并运行CoffeeScript文件,如下所示。
c:\> coffee example.coffee
执行时,CoffeeScript 文件产生以下输出。
The contents of the variable names are ::Ram,Mohammed,John
The value returned by the function is Ram 

by关键字

CoffeeScript 提供范围来定义元素列表。例如,范围 [1..10] 等价于 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 其中,每个元素递增 1、我们也可以更改此递增使用推导式的 by 关键字。

示例

以下示例演示了 CoffeeScript 提供的 for..in 推导式的 by 关键字的用法。将此代码保存在名为 by_keyword_example.coffee 的文件中
array = (num for num in [1..10] by 2)
console.log array
打开 命令提示符并编译.coffee文件,如下所示。
c:\> coffee-c by_keyword_example.coffee
在编译时,它会为您提供以下 JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
  var array, num;
  array = (function() {
    var i, results;
    results = [];
    for (num = i = 1; i <= 10; num = i += 2) {
      results.push(num);
    }
    return results;
  })();
  console.log(array);
}).call(this);
现在,再次打开 命令提示符并运行CoffeeScript文件,如下所示。
c:\> coffee by_keyword_example.coffee
执行时,CoffeeScript 文件产生以下输出。
[ 1, 3, 5, 7, 9 ] 
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4