逐句学英文-数组

Arraysarray数组

An array is a collection of data values, just as an object is.
数组和对象一样是数值的集合。

In JavaScript, you retrieve a value from an array by enclosing an index in square brackets after the array name.
在 JavaScript 中,要获取数组中的某个值,可以使用数组名,在其后加上用方括号封闭起来的小标值即可。

For example, if an array is named a and i is a nonnegative integer, a[i] is an element of array.
例如,如果一个数组名为a,i是一个非负整数,那么a[i]就是一个数组元素。

Creating Arrays 数组的创建

Arrays can be created with the Array() constructor function.
可以使用构造函数Array()来创建数组。

Once created, any number of indexed elements can easily be assigned to the array:
一旦创建了数组,就可以轻松地给数组的任何元素赋值:

var a = new Array();
a[0] = 1.2;
a[1] = "JavaScript";
a[2] = true;
a[3] = { x:1, y:3 };

Arrays can also be initialized by passing array elements to the Array() constructor. Thus, the previous array creation and initialization code can also be written:
通过把数组元素传递给 Array() 构造函数可以初始化数组,因此,前面创建数组和初始化数组的代码也可以写作:

var a = new Array(1.2, "JavaScript", true, { x:1, y:3 });
Array Literals 数组直接量

JavaScript defines a literal syntax for creating and initializing arrays.
JavaScript 定义了创建并初始化数组的直接量语法。

An array literal is a comma-separated list of values contained within square brackets.
数组直接量是一个封闭在方括号中的序列,序列中的元素由逗号分隔。

The values within the brackets are assigned sequentially to array indexes starting with zero.
括号内的值将被依次赋给数组元素,下标值从0开始。

For example, the array creation and initialization code in the previous section can also be written as:
例如,上面数组的创建和初始化都可以使用如下代码实现:

var a = [1.2, "JavaScript", true, { x:1, y:3 }];

Like object literals, array literals can be nested:
与对象直接量一样,数组直接量也可以被嵌套:

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

Also, as with object literals, the elements in array literals can be arbitrary expressions and need not be restricted to constants:
而且,与数据直接量相同,数组直接量中的元素不必仅限于常量,它可以是任意的表达式:

var base = 1024;
var table = [base, base+1, base+2, base+3];

Undefined elements can be included in an array literal by simply omitting a value between commas.
数组直接量中还可以存放未定义的元素,只要在逗号之间省去该元素的值就可以了。

For example, the following array contains five elements, including three undefined elements:
例如,下面的数组存放了5个元素,其中有3个是未定义的:

var sparseArray = [1,,,,5];
Referenceˈrefrəns参考

《JavaScript权威指南(影印版)(第5版)》 《JavaScript权威指南(第5版)》

崔凯博客
Powered by WordPress | Theme by ck web design | TOP 顶部
Copyright © 崔凯 All rights reserved. 蜀ICP备12025891号.