CoffeeScript - 范围

在上一章中,我们在CoffeeScript中看到了Arrays,在编程时我们将遇到一些场景,我们必须在数组中存储一系列数值,如下所示.

numbers = [1,2,3,4,5,6,7,8,9,10]

CoffeeScript提供了一种表达包含一系列数值的数组的简短方法,称为范围. CoffeeScript的这个功能灵感来自Ruby.

语法

范围由两个数值创建,即范围中的第一个和最后一个位置,分开by ..或....有两个点(1..4),范围包括(1,2,3,4);有三个点(1 ... 4),范围不包括结尾(1,2,3).

下面给出了CoffeeScript中范围的语法.我们将在方括号 [] 之间的范围内定义值,就像数组一样.在范围内,在存储一系列数值时,我们可以只指定由开始结束的值,而不是提供整个序列的值(开始结束 b> .. )如下所示.

range = [Begin..End]

示例

以下是CoffeeScript中范围的示例.将其保存在名为 ranges_example.coffee 的文件中.

numbers =[0..9]
console.log "The contents of the range are: "+ numbers

打开命令提示符并编译.coffee文件如下所示.

c:\> coffee -c ranges_example.coffee

在编译时,它会为您提供以下JavaScript.在这里,您可以观察到范围被转换为完整的CoffeeScript数组.

// Generated by CoffeeScript 1.10.0
(function() {
  var numbers;  numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

  console.log("The contents of the range are:: " + numbers);

}).call(this);

现在,再次打开命令提示符并运行CoffeeScript文件,如下所示.

c:\> coffee ranges_example.coffee

执行时,CoffeeScript文件产生以下输出.

The contents of the range are:: 0,1,2,3,4,5,6,7,8,9

不包括结束值

范围被编译为包含所有数字的完整数组.如果我们想要排除 end 值,那么我们必须使用三个点分隔范围的 start end 元素( ...... )如下所示.

range =[Begin...End]

示例

我们可以通过排除 end 值来重写上面的例子,如下所示.将以下内容保存在名为 range_excluding_end.coffee的文件中

numbers =[0...9]console.log "The contents of the range are:: "+ numbers

打开命令提示并编译.coffee文件,如下所示.

c:\> coffee -c ranges_example.coffee

在编译时,它会为您提供以下JavaScript.

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

  numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8];

  console.log("The contents of the range are:: " + numbers);

}).call(this);

现在,再次打开命令提示符并运行CoffeeScript文件,如下所示.

c:\> coffee ranges_example.coffee

执行时,CoffeeScript文件产生以下输出.在这里,您可以观察到最终值 9 被排除.

The contents of the range are:: 0,1,2,3,4,5,6,7,8

使用带变量的范围

我们还可以通过为变量分配起始值和结束值来定义范围.

示例

请考虑以下示例.这里我们使用变量定义了一个范围.将此代码保存在名为 range_variables.coffee的文件中

start=0
end=9
numbers =[start..end]
console.log "The contents of the range are: "+ numbers

打开命令提示符并编译.coffee文件,如下所示.

c:\> coffee -c range_variables.coffee

在编译时,它会为您提供以下JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var end, i, numbers, results, start;

  start = 0;

  end = 9;

  numbers = (function() {
    results = [];
    for (var i = start; start <= end ? i <= end : i >= end; start <= end ? i++ : i--) {
      results.push(i);
    }
    return results;
  }).apply(this);

  console.log("The contents of the range are:: " + numbers);

}).call(this);

现在,再次打开命令提示符并运行CoffeeScript文件,如下所示.

c:\> coffee range_variables.coffee

执行时,CoffeeScript文件产生以下输出.在这里,您可以观察到最终值 9 被排除在外.

The contents of the range are:: 0,1,2,3,4,5,6,7,8,9

带数组的范围

我们可以通过使用范围对数组进行切片.每当我们在数组(变量)之后立即指定范围时,CoffeeScript编译器就会将其转换为JavaScript的 slice()方法调用.

假设我们有一个具有数值的数组,比如0到9,那么我们可以检索它的前4个元素,如下所示.

 num = [1 ,2,3,4,5,6,7,8,9] 
 data = num [0..5]

负值表示结尾的元素,例如,-1表示9.如果我们指定负数3后跟两个点,则将提取数组的最后三个元素.

data = num [-3 ..]

如果我们只指定数组范围内的两个点为 num [..] ,然后将提取完整的数组.我们还可以使用如下所示的范围将数组段替换为其他元素.

num [2..6] = [13,14, 15,16,17]

示例

以下示例演示了数组范围的使用.将此代码保存在名为 range_arrays.coffee的文件中

#slicing an array using ranges
num  = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data = num[0..5]
console.log "The first four elements of the array : "+data


#Using negative values
data = num[-3..]
console.log "The last 3 elements of the array : "+data

#Extracting the whole array
console.log "Total elements of the array : "+num[..]


#Replacing the elements of an array
num[2..6] = [13,14,15,16,17]
console.log "New array : "+num

打开命令提示符并编译.coffee文件,如下所示.

c:\> coffee -c range_arrays.coffee

在编译时,它会为您提供以下JavaScript.在这里,您可以观察到所有范围都转换为JavaScript的slice()方法调用.

// Generated by CoffeeScript 1.10.0
(function() {
  var data, num, ref;

  num = [1, 2, 3, 4, 5, 6, 7, 8, 9];

  data = num.slice(0, 6);

  console.log("The first four elements of the array : " + data);

  data = num.slice(-3);

  console.log("The last 3 elements of the array : " + data);

  console.log("Total elements of the array : " + num.slice(0));

  [].splice.apply(num, [2, 5].concat(ref = [13, 14, 15, 16, 17])), ref;

  console.log("New array : " + num);

}).call(this);

现在,再次打开命令提示符并运行CoffeeScript文件,如下所示.

c:\> coffee range_arrays.coffee

执行时,CoffeeScript文件产生以下输出.在这里,你可以观察到最终值 9 被排除在外.

The first four elements of the array : 1,2,3,4,5,6
The last 3 elements of the array : 7,8,9
Total elements of the array : 1,2,3,4,5,6,7,8,9
New array : 1,2,13,14,15,16,17,8,9

字符串范围

我们也可以使用带字符串的范围.如果我们在字符串之后指定范围,那么CoffeeScript会对它们进行切片并返回一个新的字符子集.

示例

以下示例演示了范围的使用与弦乐.这里我们创建了一个字符串,并使用范围从中提取了一个子字符串.将此代码保存在名为 ranges_with_strings.coffee的文件中

my_string = "Welcome to it352"
new_string = my_string[0..10]
console.log new_string

打开命令提示符并编译.coffee文件如下所示.

c:\> coffee -c ranges_with_strings.coffee

在编译时,它会为您提供以下JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var my_string, new_string;

  my_string = "Welcome to IT屋";

  new_string = my_string.slice(0, 6);

  console.log(new_string);

}).call(this);

现在,再次打开命令提示符并运行CoffeeScript文件,如下所示.

c:\> coffee ranges_with_strings.coffee

执行时,CoffeeScript文件产生以下输出.在这里,你可以观察到最终值 9 被排除在外.

Welcome to

对范围的理解

作为对象和数组,我们还可以使用理解来迭代范围的元素.

示例

以下是对范围使用理解的示例.在这里,我们创建了一个范围,并使用理解来检索其中的元素.将此代码保存在名为 comprehensions_over_ranges.coffee

的文件中.

numbers =[0..9]
console.log "The elements of the range are: "
console.log num for num in numbers

打开命令提示符并编译.coffee文件,如下所示.

c:\> coffee -c comprehensions_over_ranges.coffee

在编译时,它会为您提供以下JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var i, len, num, numbers;

  numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

  console.log("The elements of the range are: ");

  for (i = 0, len = numbers.length; i < len; i++) {
    num = numbers[i];
    console.log(num);
  }

}).call(this);

现在,再次打开命令提示符并运行CoffeeScript文件,如下所示.

c:\> coffee comprehensions_over_ranges.coffee

执行时,CoffeeScript文件产生以下输出.在这里,您可以观察到最终值 9 被排除.

The elements of the range are:
0
1
2
3
4
5
6
7
8

以同样的方式我们也可以使用理解的关键字来改变这个增量.

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