CoffeeScript 范围
在上一章中,我们已经看到了 CoffeeScript 中的数组,在编程时我们将面临一些场景,我们必须将一系列数值存储在一个数组中,如下所示。
numbers =[1,2,3,4,5,6,7,8,9,10]
CoffeeScript 提供了一种更短的方式来表达包含数值序列的数组,称为
范围。 CoffeeScript 的这个特性灵感来自 Ruby。
语法
范围由两个数值创建,范围中的第一个和最后一个位置,用 .. 或 ... 4);三个点 (1...4),范围不包括结尾 (1, 2, 3)。
下面给出了 CoffeeScript 中范围的语法。我们将定义方括号
[ ] 之间范围内的值,就像数组一样。在范围中,在存储数值序列时,我们可以只指定由两个点分隔的
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 值,那么我们必须使用三个点(
...) 如下所示。
示例
我们可以通过排除
end 值来重写上面的示例,如下所示。将以下内容保存在名为
range_ exclude_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后跟两个点,则将提取数组的最后三个元素。
如果我们只将数组范围内的两个点指定为
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 lidihuo"
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 lidihuo";
new_string = my_string.slice(0, 6);
console.log(new_string);
}).call(this);
现在,再次打开
命令提示符并运行CoffeeScript文件,如下所示。
c:\> coffee ranges_with_strings.coffee
执行时,CoffeeScript 文件会产生以下输出。在这里,您可以观察到排除了结束值
9。
对范围的理解
作为对象和数组,我们还可以使用推导式迭代范围的元素。
示例
以下是在范围内使用推导式的示例。在这里,我们创建了一个范围并使用推导检索其中的元素。将此代码保存在名为
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