目录

CoffeeScript - 理解( Comprehensions)

在上一章中,我们学习了CoffeeScript提供的各种循环,以及它的变体。 除此之外,CoffeeScript还提供了额外的循环结构,称为comprehensions

如果我们明确地添加可选的guard子句和当前数组索引的值,这些理解将替换其他编程语言中的for循环。 使用理解,我们可以迭代数组和对象,迭代数组的表达式是表达式,我们可以在函数中返回它们或分配给变量。

S.No. 声明和说明
1 for..in comprehensions

for..in理解是CoffeeScript中理解的基本形式,使用它我们可以迭代列表或数组的元素。

2 for..of comprehensions

就像Arrays CoffeeScriptScript提供了一个容器来存储称为对象的键值对。 我们可以使用CoffeeScript提供的for..of理解来迭代对象。

3 list comprehensions

CoffeeScript中的list推导用于将对象数组映射到另一个数组。

理解指数

列表/元素数组具有可用于理解的索引。 您可以使用变量在结合中使用它,如下所示。

for student,i in [element1, element2, element3]

例子 (Example)

以下示例演示了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 

打开command prompt并编译.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);

现在,再次打开command prompt并运行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 

Postfix形式的理解

就像postfix 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}
show example

分配给变量

我们用于迭代数组的理解可以分配给变量,也可以由函数返回。

例子 (Example)

考虑下面给出的例子。 在这里,您可以观察到我们使用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 <b class="notranslate">names</b> are ::"+names
   #Returning the comprehension
   return x for x in student
console.log "The value returned by the function is "+my_function() 

打开command prompt并编译.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);

现在,再次打开command prompt并运行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关键字。

例子 (Example)

以下示例演示了CoffeeScript提供的for..in理解的by关键字的用法。 将此代码保存在名为by_keyword_example.coffee的文件中

array = (num for num in [1..10] by 2)
console.log array

打开command prompt并编译.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);

现在,再次打开command prompt并运行CoffeeScript文件,如下所示。

c:\> coffee by_keyword_example.coffee

执行时,CoffeeScript文件生成以下输出。

[ 1, 3, 5, 7, 9 ] 
↑回到顶部↑
WIKI教程 @2018