Coffeescript 简明教程

CoffeeScript - Arrays

Array 对象允许您将多个值存储在单个变量中。它存储一个固定大小的元素顺序集合,该元素具有相同的类型。数组用于存储数据集合,但通常将数组视为同一类型的变量集合更有用。

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Syntax

要创建数组,我们必须使用 new 运算符进行实例化,如下所示。

To create an array, we have to instantiate it using the new operator as shown below.

array = new (element1, element2,....elementN)

Array() 构造函数接受字符串或整数类型的列表。我们还可以通过向其构造函数传递单个整数来指定数组的长度。

The Array() constructor accepts the list of string or integer types. We can also specify the length of the array by passing a single integer to its constructor.

我们还可以在方括号 ( [ ] ) 中仅提供其元素列表来定义数组,如下所示。

We can also define an array by simply providing the list of its elements in the square braces ([ ]) as shown below.

array = [element1, element2, ......elementN]

Example

以下是 CoffeeScript 中定义数组的示例。将以下代码保存在一个名叫 array_example.coffee 的文件中

Following is an example of defining an array in CoffeeScript. Save this code in a file with name array_example.coffee

student = ["Rahman","Ramu","Ravi","Robert"]

打开 command prompt 并按照以下所示编译 .coffee 文件。

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c array_example.coffee

编译后,它会给你以下 JavaScript。

On compiling, it gives you the following JavaScript.

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

  student = ["Rahman", "Ramu", "Ravi", "Robert"];

}).call(this);

New line instead of comma

我们还可以通过在每一行中创建新元素并保持适当缩进来删除数组元素之间的逗号 (,),如下所示。

We can also remove the comma (,) between the elements of an array by creating each element in a new line by maintaining proper indentation as shown below.

student = [
  "Rahman"
  "Ramu"
  "Ravi"
  "Robert"
  ]

Comprehensions over arrays

我们可以使用理解检索数组的值。

We can retrieve the values of an array using comprehensions.

Example

以下示例演示了使用理解检索数组的元素。将以下代码保存在一个名叫 array_comprehensions.coffee 的文件中

The following example demonstrates the retrieval of elements of an array using comprehensions. Save this code in a file with name array_comprehensions.coffee

students = [ "Rahman", "Ramu", "Ravi", "Robert" ]
console.log student for student in students

打开 command prompt 并按照以下所示编译 .coffee 文件。

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c array_comprehensions.coffee

编译后,它会给你以下 JavaScript。

On compiling, it gives you the following JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var i, len, student, students;

  students = ["Rahman", "Ramu", "Ravi", "Robert"];

  for (i = 0, len = students.length; i − len; i++) {
    student = students[i];
    console.log(student);
  }

}).call(this);

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

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee array_comprehensions.coffee

执行后,CoffeeScript 文件产生以下输出。

On executing, the CoffeeScript file produces the following output.

Rahman
Ramu
Ravi
Robert

与其他编程语言中的数组不同,CoffeeScript 中的数组可以具有多种类型的数据,即字符串和数字。

Unlike the Arrays in other programming languages the arrays in CoffeeScript can have multiple types of data i.e. both string and numericals.

Example

以下是一个包含多种类型数据的 CoffeeScript 数组示例。

Here is an example of a CoffeeScript array holding multiple types of data.

students = [ "Rahman", "Ramu", "Ravi", "Robert",21 ]