diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/it/web/javascript/reference/errors/invalid_array_length | |
parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip |
initial commit
Diffstat (limited to 'files/it/web/javascript/reference/errors/invalid_array_length')
-rw-r--r-- | files/it/web/javascript/reference/errors/invalid_array_length/index.html | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/errors/invalid_array_length/index.html b/files/it/web/javascript/reference/errors/invalid_array_length/index.html new file mode 100644 index 0000000000..61ee1cba5f --- /dev/null +++ b/files/it/web/javascript/reference/errors/invalid_array_length/index.html @@ -0,0 +1,77 @@ +--- +title: 'RangeError: invalid array length' +slug: Web/JavaScript/Reference/Errors/Invalid_array_length +tags: + - Errori + - JavaScript + - RangeError +translation_of: Web/JavaScript/Reference/Errors/Invalid_array_length +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="Messaggio">Messaggio</h2> + +<pre class="syntaxbox">RangeError: invalid array length (Firefox) +RangeError: Invalid array length (Chrome) +RangeError: Invalid array buffer length (Chrome) +</pre> + +<h2 id="Tipo_di_errore">Tipo di errore</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="Cosa_è_andato_storto">Cosa è andato storto?</h2> + +<p>Un invalid array length può comparire in queste situazioni:</p> + +<ul> + <li>Quando viene creato un {{jsxref("Array")}} o un {{jsxref("ArrayBuffer")}} che ha lunghezza negativa o maggiore uguale a 2<sup>32</sup>, oppure</li> + <li>quando viene impostata la proprietà {{jsxref("Array.length")}} a un valore che è negativo o maggiore uguale a 2<sup>32</sup>.</li> +</ul> + +<p>Perché la lunghezza di <code>Array</code> e <code>ArrayBuffer</code> è limitata? La proprietà <code>length</code> di un <code>Array </code>o di un <code>ArrayBuffer</code> è rappresentata da un intero di 32-bit senza segno, che quindi permette di memorizzare valori nell'intervallo da 0 a 2<sup>32 </sup>- 1.</p> + +<p>Se stai creando un <code>Array</code> utilizzando il costruttore, potresti invece utilizzare la notazione letterale dove il primo argomento è interpretato come la lunghezza dell' <code>Array</code>.</p> + +<p>Altrimenti, potresti voler bloccare la lunghezza prima di settare la proprietà length, o utilizzarla come argomento del costruttore.</p> + +<h2 id="Esempi">Esempi</h2> + +<h3 id="Casi_non_validi">Casi non validi</h3> + +<pre class="brush: 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; // setta -1 alla proprietà length + +let b = new Array(Math.pow(2, 32) - 1); +b.length = b.length + 1; // setta 2^32 alla proprietà length +</pre> + +<h3 id="Casi_validi">Casi validi</h3> + +<pre class="brush: 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 è la notazione esadecimale di 2^32 - 1 +// che può essere riscritta come (-1 >>> 0) +</pre> + +<h2 id="Guarda_anche">Guarda anche</h2> + +<ul> + <li>{{jsxref("Array")}}</li> + <li>{{jsxref("Array.length")}}</li> + <li>{{jsxref("ArrayBuffer")}}</li> +</ul> |