aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html
blob: de5862921d1718c1129a6f078729314021de705a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
title: Array.length
slug: Web/JavaScript/Reference/Global_Objects/Array/length
tags:
  - JavaScript
  - 参考
  - 属性
  - 数组
translation_of: Web/JavaScript/Reference/Global_Objects/Array/length
---
<p>{{JSRef}}</p>

<p><code><strong>length</strong></code><code>Array</code>的实例属性。返回或设置一个数组中的元素个数。该值是一个无符号 32-bit 整数,并且总是大于数组最高项的下标。</p>

<div>{{EmbedInteractiveExample("pages/js/array-length.html")}}</div>

<h2 id="Description" name="Description">描述</h2>

<p><code>length</code> 属性的值是一个 0 到 2<sup>32</sup>-1 的整数。</p>

<pre class="brush: js">var namelistA = new Array(4294967296); // 2的32次方 = 4294967296
var namelistC = new Array(-100) // 负号

console.log(namelistA.length); // RangeError: 无效数组长度
console.log(namelistC.length); // RangeError: 无效数组长度



var namelistB = [];
namelistB.length = Math.pow(2,32)-1; //set array length less than 2 to the 32nd power
console.log(namelistB.length);

// 4294967295
</pre>

<p>你可以设置 <code>length</code> 属性的值来截断任何数组。当通过改变<code>length</code>属性值来扩展数组时,实际元素的数目将会增加。例如:将一个拥有 2 个元素的数组的 <code>length</code> 属性值设为 3 时,那么这个数组将会包含3个元素,并且,第三个元素的值将会是 <code>undefined</code></p>

<pre><code>var arr = [1, 2, 3];
printEntries(arr);

arr.length = 5; // set array length to 5 while currently 3.
printEntries(arr);

function printEntries(arr) {
  var goNext = true;
  var entries = arr.entries();
  while (goNext) {
    var result = entries.next();
    if (result.done !== true) {
      console.log(result.value[1]);
      goNext = true;
    } else
      goNext = false;
  }
  console.log('=== printed ===');
}

// 1
// 2
// 3
// === printed ===
// 1
// 2
// 3
// undefined
// undefined
// === printed ===</code></pre>

<p>但是, <code>length</code> 属性不一定表示数组中定义值的个数。了解更多:<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties">长度与数值下标属性之间的关系</a></p>

<p>{{js_property_attributes(1, 0, 0)}}</p>

<ul>
 <li><code>Writable</code> :如果设置为<code>false</code>,该属性值将不能被修改。</li>
 <li><code>Configurable</code> :如果设置为<code>false</code>,删除或更改任何属性都将会失败。</li>
 <li><code>Enumerable</code> :如果设置为 <code>true</code> ,属性可以通过迭代器<a href="zh-CN/docs/Web/JavaScript/Reference/Statements/for">for</a><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a>进行迭代。</li>
</ul>

<h2 id="Examples" name="Examples">示例 </h2>

<h3 id="Example:_Iterating_over_an_array" name="Example:_Iterating_over_an_array">遍历数组</h3>

<p>下面的例子中,通过数组下标遍历数组元素,并把每个元素的值修改为原值的2倍。</p>

<pre class="brush: js line-numbers  language-js">var numbers = [1, 2, 3, 4, 5];
var length = numbers.length;
for (var i = 0; i &lt; length; i++) {
  numbers[i] *= 2;
}
// 遍历后的结果 [2, 4, 6, 8, 10]</pre>

<h3 id="Example:_Shortening_an_array" name="Example:_Shortening_an_array">截断数组</h3>

<p>下面的例子中,如果数组长度大于 3,则把该数组的长度截断为 3 。</p>

<pre class="brush: js line-numbers  language-js">var numbers = [1, 2, 3, 4, 5];

if (numbers.length &gt; 3) {
  numbers.length = 3;
}

console.log(numbers); // [1, 2, 3]
console.log(numbers.length); // 3</pre>

<h2 id="规范">规范</h2>

<table>
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.5.2', 'Array.length')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-properties-of-array-instances-length', 'Array.length')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-properties-of-array-instances-length', 'Array.length')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>


<p>{{Compat("javascript.builtins.Array.length")}}</p>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
</ul>