n JavaScript, the Array property of the global object is a constructor for array instances.(在JavaScript,全局对象数组属性是构造数组对象的构造器。)

 
An array is a JavaScript object. Note that you shouldn't use it as an associative array, use Object instead.(一个数组是JavaScript对象。注意你不应该把它作为关联数组来使用,应该使用对象来代替。)
 
Creating an Array(创建数组)
 
The following example creates an array, msgArray, with a length of 0, then assigns values to msgArray[0] and msgArray[99], changing the length of the array to 100.(下面的例子创建了一个长度为0的数组msgArray,然后把数值赋给msgArray[0]到msgArray[99],把数组的长度改变成100。)
 
  1. <script> 
  2. var msgArray = new Array(); 
  3. msgArray[0] = "Hello"; 
  4. msgArray[99] = "world"; 
  5.  
  6. if (msgArray.length == 100){ 
  7.   document.write("The length is 100."); 
  8. </script> 
 Creating a Two-dimensional Array(创建一个二维数组)
 
The following creates chess board as a two dimensional array of strings.(下面的例子创建了二维字符串数组来表示国际象棋棋盘) The first move is made by copying the 'P' in 6,4 to 4,4. The position 6,4 is left blank.(第一步是拷贝6,4的'P'到4,4。位置6,4是空白的)
 
  1. <script> 
  2. var board = 
  3. [ ['R','N','B','Q','K','B','N','R'], 
  4. ['P','P','P','P','P','P','P','P'], 
  5. [' ',' ',' ',' ',' ',' ',' ',' '], 
  6. [' ',' ',' ',' ',' ',' ',' ',' '], 
  7. [' ',' ',' ',' ',' ',' ',' ',' '], 
  8. [' ',' ',' ',' ',' ',' ',' ',' '], 
  9. ['p','p','p','p','p','p','p','p'], 
  10. ['r','n','b','q','k','b','n','r']]; 
  11. document.write(board.join('\n') + '\n\n' + "<br/>"); 
  12.  
  13. // Move King's Pawn forward 2 
  14. board[4][4] = board[6][4]; 
  15. board[6][4] = ' '; 
  16. document.write(board.join('\n')); 
  17. </script> 
 Here is the output:(下面是输出:)
R,N,B,Q,K,B,N,R
P,P,P,P,P,P,P,P
 , , , , , , ,
 , , , , , , ,
 , , , , , , ,
 , , , , , , ,
p,p,p,p,p,p,p,p
r,n,b,q,k,b,n,r
 
R,N,B,Q,K,B,N,R
P,P,P,P,P,P,P,P
 , , , , , , ,
 , , , , , , ,
 , , , ,p, , ,
 , , , , , , ,
p,p,p,p, ,p,p,p
r,n,b,q,k,b,n,r
 
Accessing array elements(访问数组元素)
Array elements are nothing less than object properties, so they are accessed as such.(数组元素和对象属性是一样的,所以它们是这样被访问的。)
 
 
  1. <script> 
  2. var myArray = new Array("Wind""Rain""Fire"); 
  3. document.write(myArray[0] + "<br/>"); // "Wind" 
  4. document.write(myArray[1] + "<br/>"); // "Rain" 
  5. // etc. 
  6. document.write(myArray.length + "<br/>"); // 3 
  7.  
  8. // Even if indices are properties, the following notation throws a syntax error(即使索引是属性,下面符号将会抛出语法错误) 
  9. //document.write(myArray.2 + "<br/>"); 
  10.  
  11. // It should be noted that in JavaScript, object property names are strings. Consequently, 
  12. //应该注意的是,在JavaScript中,对象的属性名称是字符串。因此, 
  13. document.write((myArray[0] === myArray["0"]) + "<br/>"); 
  14. document.write((myArray[1] === myArray["1"]) + "<br/>"); 
  15. // etc. 
  16.  
  17. // However, this should be considered carefully(无论如何,这都要仔细地思考) 
  18. document.write(myArray[02] + "<br/>"); // "Fire". The number 02 is converted as the "2" string 
  19. document.write(myArray["02"] + "<br/>"); // undefined. There is no property named "02" 
  20. </script> 
 Relationship between length and numerical properties(长度和数值属性们的关系)
 
An array's length property and numerical properties are connected. (数组长度属性和数值属性是关联的)Here is some code explaining how this relationship works.(下面这些代码解释了这些关系是怎么工作的。)
 
  1. <script> 
  2. var a = []; 
  3.  
  4. a[0] = 'a'; 
  5. console.log(a[0]); // 'a' 
  6. console.log(a.length); // 1 
  7.  
  8. a[1] = 32; 
  9. console.log(a[1]); // 32 
  10. console.log(a.length); // 2 
  11.  
  12. a[13] = 12345; 
  13. console.log(a[13]); // 12345 
  14. console.log(a.length); // 14 
  15.  
  16. a.length = 10
  17. console.log(a[13]); // undefined, when reducing the length elements after length+1 are removed 
  18. console.log(a.length); // 10 
  19. </script> 
Creating an array using the result of a match(使用match的结果创建对象)
 
The result of a match between a regular expression and a string can create an array. (正则表达式和字符串的match的结果可以创建一个字符串。)This array has properties and elements that provide information about the match.(这个数组有能提供关于这个match的信息的属性和元素。) An array is the return value of RegExp.exec, String.match, and String.replace.(RegExp.exec,String.match,String.replace返回了数组) To help explain these properties and elements, look at the following example and then refer to the table below:(为了解释这些属性和元素,请看下面的例子,接下来看看下面的表格:)
 
  1. // Match one d followed by one or more b's followed by one d 
  2. // Remember matched b's and the following d 
  3. // Ignore case 
  4.  
  5. var myRe = /d(b+)(d)/i; 
  6. var myArray = myRe.exec("cdbBdbsbz"); 
111