diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/length')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/array/length/index.html | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html index 7b126495fb..8f17e9af1f 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html @@ -82,25 +82,25 @@ function printEntries(arr) { <p>下面的例子中,通过数组下标遍历数组元素,并把每个元素的值修改为原值的2倍。</p> -<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> numbers <span class="operator token">=</span> <span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">,</span> <span class="number token">5</span><span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="keyword token">var</span> length <span class="operator token">=</span> numbers<span class="punctuation token">.</span>length<span class="punctuation token">;</span> -<span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span> i <span class="operator token"><</span> length<span class="punctuation token">;</span> i<span class="operator token">++</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - numbers<span class="punctuation token">[</span>i<span class="punctuation token">]</span> <span class="operator token">*</span><span class="operator token">=</span> <span class="number token">2</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span> -<span class="comment token">// 遍历后的结果 [2, 4, 6, 8, 10]</span></code></pre> +<pre class="brush: js line-numbers language-js"><code class="language-js">var numbers = [1, 2, 3, 4, 5]; +var length = numbers.length; +for (var i = 0; i < length; i++) { + numbers[i] *= 2; +} +// 遍历后的结果 [2, 4, 6, 8, 10]</code></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"><code class="language-js"><span class="keyword token">var</span> numbers <span class="operator token">=</span> <span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">,</span> <span class="number token">5</span><span class="punctuation token">]</span><span class="punctuation token">;</span> +<pre class="brush: js line-numbers language-js"><code class="language-js">var numbers = [1, 2, 3, 4, 5]; -<span class="keyword token">if</span> <span class="punctuation token">(</span>numbers<span class="punctuation token">.</span>length <span class="operator token">></span> <span class="number token">3</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - numbers<span class="punctuation token">.</span>length <span class="operator token">=</span> <span class="number token">3</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span> +if (numbers.length > 3) { + numbers.length = 3; +} -console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>numbers<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// [1, 2, 3]</span> -console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>numbers<span class="punctuation token">.</span>length<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 3</span></code></pre> +console.log(numbers); // [1, 2, 3] +console.log(numbers.length); // 3</code></pre> <h2 id="规范">规范</h2> |