mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-11 00:54:06 +08:00
53 lines
3.6 KiB
HTML
53 lines
3.6 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<h2 id="错误信息">错误信息</h2>
|
||
<pre><code class="language-javascript">RangeError: invalid array length (Firefox)
|
||
RangeError: Invalid array length (Chrome)
|
||
RangeError: Invalid array buffer length (Chrome)
|
||
</code></pre>
|
||
<h2 id="错误类型">错误类型</h2>
|
||
<p><a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误,当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a></p>
|
||
<h2 id="什么地方出错了">什么地方出错了?</h2>
|
||
<p>无效的数组错误长度通常会在以下情形中出现:</p>
|
||
<ul>
|
||
<li>当创建一个长度为负数或者长度大于等于2<sup>32 </sup>的<sup> </sup><a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a> 或者 <a href="Reference/Global_Objects/ArrayBuffer" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a> 时。</li>
|
||
<li>当设置 <a href="Reference/Global_Objects/Array/length" title="length 是Array的实例属性。返回或设置一个数组中的元素个数。该值是一个无符号 32-bit 整数,并且总是大于数组最高项的下标。"><code>Array.length</code></a> 属性为负数或者长度大于等于2<sup>32 </sup>时。</li>
|
||
</ul>
|
||
<p>为什么 Array(数组) 和 ArrayBuffer(数组缓冲区) 的长度会受到限制?因为 Array 和 ArrayBuffer 的 length(长度) 属性被定义为一个32位无符号整形(unsigned 32-bit integer)的值,所以它只能存储 0 - 2<sup>32</sup>-1 之间的数。</p>
|
||
<p>当你使用构造函数来创建一个数组的时候,你可能想使用字面值的形式,第一个参数会被解释为数组的长度。</p>
|
||
<p>或者说,你想要在设置数组之前确定它的长度,或把它作为一个构造函数的参数。</p>
|
||
<h2 id="示例">示例</h2>
|
||
<h3 id="错误的示例">错误的示例</h3>
|
||
<pre><code class="language-js example-bad">new Array(Math.pow(2, 40))
|
||
new Array(-1)
|
||
new ArrayBuffer(Math.pow(2, 32))
|
||
new ArrayBuffer(-1)
|
||
|
||
let a = [];
|
||
a.length = a.length - 1; // 将 length 属性的值设置为 -1
|
||
|
||
let b = new Array(Math.pow(2, 32) - 1);
|
||
b.length = b.length + 1; // 将 length 属性的值设置为 2^32
|
||
</code></pre>
|
||
<h3 id="正确的示例">正确的示例</h3>
|
||
<pre><code class="language-js example-good">[ Math.pow(2, 40) ] // [ 1099511627776 ]
|
||
[ -1 ] // [ -1 ]
|
||
new ArrayBuffer(Math.pow(2, 32) - 1)
|
||
new ArrayBuffer(0)
|
||
|
||
let a = [];
|
||
a.length = Math.max(0, a.length - 1);
|
||
|
||
let b = new Array(Math.pow(2, 32) - 1);
|
||
b.length = Math.min(0xffffffff, b.length + 1);
|
||
|
||
// 0xffffffff 是 2^32 - 1 的 十六进制 表示方式
|
||
// 它也可以被写作 (-1 >>> 0)
|
||
</code></pre>
|
||
<h2 id="相关页面">相关页面</h2>
|
||
<ul>
|
||
<li><a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a></li>
|
||
<li><a href="Reference/Global_Objects/Array/length" title="length 是Array的实例属性。返回或设置一个数组中的元素个数。该值是一个无符号 32-bit 整数,并且总是大于数组最高项的下标。"><code>Array.length</code></a></li>
|
||
<li><a href="Reference/Global_Objects/ArrayBuffer" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a></li>
|
||
</ul>
|
||
</article> |