aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/statements/let
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:42:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:42:17 -0500
commitda78a9e329e272dedb2400b79a3bdeebff387d47 (patch)
treee6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/it/web/javascript/reference/statements/let
parent1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff)
downloadtranslated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz
translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2
translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip
initial commit
Diffstat (limited to 'files/it/web/javascript/reference/statements/let')
-rw-r--r--files/it/web/javascript/reference/statements/let/index.html208
1 files changed, 208 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/statements/let/index.html b/files/it/web/javascript/reference/statements/let/index.html
new file mode 100644
index 0000000000..7900b7673a
--- /dev/null
+++ b/files/it/web/javascript/reference/statements/let/index.html
@@ -0,0 +1,208 @@
+---
+title: let
+slug: Web/JavaScript/Reference/Statements/let
+translation_of: Web/JavaScript/Reference/Statements/let
+---
+<div>{{jsSidebar("Statements")}}</div>
+
+<p>L'istruzione <strong>let </strong>dichiara una variabile locale nel blocco di codice e, facoltativamente, la inizializza ad un valore.</p>
+
+<div>{{EmbedInteractiveExample("pages/js/statement-let.html")}}</div>
+
+<div class="hidden">Il codice sorgente di questo esempio interattivo è salvato in un repository GitHub. Se tu volessi contribuire al progetto 'interactive examples', puoi clonare <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> e sottomettere una pull request.</div>
+
+<h2 id="Summary" name="Summary">Sintassi</h2>
+
+<pre class="syntaxbox notranslate">let <var>var1</var> [= <var>value1</var>] [, <var>var2</var> [= <var>value2</var>]] [, ..., <var>varN</var> [= <var>valueN</var>]];
+</pre>
+
+<h3 id="Parameters" name="Parameters">Parametri</h3>
+
+<dl>
+ <dt><code><var>var1</var></code>, <code><var>var2</var></code>, …, <code><var>varN</var></code></dt>
+ <dd>Il nome della variabile o delle variabili da dichiarare. Devono essere identificatori JavaScript validi.</dd>
+ <dt><code><var>value1</var></code>, <code><var>value2</var></code>, …, <code><var>valueN</var></code> {{optional_inline}}</dt>
+ <dd>Per ogni variabile definita, è possibile specificare il valore iniziale usando qualunque espressione JavaScript valida.</dd>
+</dl>
+
+<h2 id="Description" name="Description">Descrizione</h2>
+
+<p><strong><code>let</code></strong> permette di dichiarare variabili limitandone la visibilità ad un {{jsxref("statements/block", "blocco di codice", "", 1)}}, o ad un'espressione in cui è usata, contrariamente alla parola chiave <a href="/it/docs/JavaScript/Reference/Statements/var" title="JavaScript/Reference/Statements/var"><code>var</code></a>, che invece definisce una variabile globalmente in uno script o localmente in una funzione a prescindere dal blocco di codice. L'altra differenza tra {{jsxref("statements/var", "var")}} e <code>let</code> è che la seconda è inizializzata ad un valore solo quando un parser la evaluta (vedi sotto).</p>
+
+<h2 id="Block_scoping" name="Block_scoping">Esempi</h2>
+
+<h3 id="Regole_di_visibilità">Regole di visibilità</h3>
+
+<p>Le variabili inizializzate da <code>let</code> sono legate al blocco dell'espressione in cui sono dichiarate. Il suo funzionamento è del tutto simile a quello di <code>var</code>, ma le variabili definite da quest'ultima istruzione sono sempre variabili globali a livello di programma o di funzione, hanno quindi una visibilità più ampia.</p>
+
+<pre class="notranslate">function varTest() {
+ var x = 1;
+ {
+ var x = 2; // same variable!
+ console.log(x); // 2
+ }
+ console.log(x); // 2
+}
+
+function letTest() {
+ let x = 1;
+ {
+ let x = 2; // different variable
+ console.log(x); // 2
+ }
+ console.log(x); // 1
+}</pre>
+
+<p>Dichiarare nuovamente la stessa variabile con <code>let</code> restituisce l'errore <code><a href="/it/docs/JavaScript/Reference/Global_Objects/TypeError" title="TypeError">TypeError</a>.</code></p>
+
+<pre class="brush: js notranslate"><code>
+if (x) {
+ let foo;
+ let foo; // lancia un errore TypeError.
+}</code></pre>
+
+<p>Se usata al livello più alto di programma, <code>let</code>, a differenza di <code>var</code>, non crea una proprietà seuul'oggetto globale. Per esempio:</p>
+
+<pre class="notranslate">var x = 'global';
+let y = 'global';
+console.log(this.x); // "global"
+console.log(this.y); // undefined</pre>
+
+<p>Potresti imbatterti in errori in un blocco <a href="/it/docs/JavaScript/Reference/Statements/switch" title="switch"><code>switch</code></a> perché i casi non vengono separati ma fanno parte tutti dello stesso blocco.</p>
+
+<pre class="brush: js notranslate"><code>
+switch (x) {
+ case 0:
+ let foo;
+ break;
+
+ case 1:
+ let foo; // TypeError for redeclaration.
+ break;
+}</code></pre>
+
+<h2 id="Examples" name="Examples"><code>Esempi</code></h2>
+
+<p>Una <em><code>let</code> expression</em> può essere usata per limitare la visibilità della variabile dichiarata alla sola espressione chiamata.</p>
+
+<pre class="brush: js notranslate"><code>
+var a = 5;
+let(a = 6) alert(a); // 6
+alert(a); // 5</code></pre>
+
+<p>Usando <code>let</code> in un blocco di codice ne limitiamo la visibilità al solo blocco racchiuso. Nel codice di esempio nota come le due assegnazioni nel blocco <code>if</code> diano due risultati diversi.</p>
+
+<pre class="brush: js notranslate"><code>
+var a = 5;
+var b = 10;
+
+if (a === 5) {
+ let a = 4; // La visibilità è dentro il blocco if
+ var b = 1; // La visibilità è dentro la funzione
+
+ console.log(a); // 4
+ console.log(b); // 1
+}
+
+console.log(a); // 5
+console.log(b); // 1</code></pre>
+
+<p>Puoi usare <strong><code>let</code></strong> come iteratore in un ciclo <strong><code>for</code></strong> invece di usare una nuova variabile globale.</p>
+
+<pre class="brush: js notranslate"><code>
+for (let i = 0; i&lt;10; i++) {
+ alert(i); // 1, 2, 3, 4 ... 9
+}
+
+alert(i); // i non è definita</code></pre>
+
+<p>Quando lavori con i costruttori puoi usare <code>let</code> per creare in'interfaccia privata senza chiusure.</p>
+
+<pre class="brush: js notranslate"><code>
+/*\
+|*|
+|*|  :: Una API pubblica e riutilizzabile per i costruttori ... ::
+|*|
+\*/
+
+let (
+  switchScope = function (oOwner, fConstructor) {
+    return oOwner &amp;&amp; oOwner.constructor === fConstructor ? oOwner : this;
+  }
+) {
+  function buildIndoors (fConstructor) {
+    const oPrivate = new fConstructor(this);
+    this.getScope = oPrivate.getScope = switchScope.bind(this, oPrivate);
+    return oPrivate;
+  }
+}
+
+/*\
+|*|
+|*|  :: Use of the *let* statement in order to create a private interface without closures... ::
+|*|
+\*/
+
+let (
+
+  /* "Secrets" is the constructor of the private interface */
+
+  Secrets = function Secrets (oPublic /* (the public interface) */) {
+    /* setting a private property... */
+    this.password = Math.floor(Math.random() * 1e16).toString(36);
+    /* you can also store the public interface into a private property... */
+    /* this.publicInterface = oPublic; */
+    alert("I\'m getting a public property from a private constructor...: somePublicProperty: " + oPublic.somePublicProperty);
+  }
+
+) {
+
+  /* "User" is the constructor of the public interface */
+
+  function User (sNick) {
+    /* setting a public property... */
+    this.somePublicProperty = "Hello World!";
+    const oPrivate = this.createScope(Secrets); /* (the private interface) */
+    /* setting a public property... */
+    this.user = sNick;
+    alert("I\'m getting a private property from a public constructor...: password: " + oPrivate.password);
+  }
+
+  User.prototype.somePublicMethod = function () {
+    const oPrivate = this.getScope(Secrets); /* (the private interface) */
+    alert("I\'m getting a public property from a public method...: user: " + this.user);
+    alert("I\'m getting a private property from a public method...: password: " + oPrivate.password);
+    oPrivate.somePrivateMethod();
+  };
+
+  Secrets.prototype.somePrivateMethod = function () {
+    const oPublic = this.getScope(); /* (the public interface) */
+    alert("I\'m getting a public property from a private method...: user: " + oPublic.user);
+    alert("I\'m getting a private property from a private method...: password: " + this.password);
+  };
+
+  /* ...creating a mutual access... */
+
+  User.prototype.createScope = buildIndoors;
+}
+
+/* out of the *let* statement you have not access to the private interface! */
+
+const johnSmith = new User("John Smith");
+johnSmith.somePublicMethod();</code></pre>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.statements.let")}}</p>
+
+<h2 id="See_also" name="See_also"></h2>
+
+<h2 id="See_also" name="See_also"><code>See also</code></h2>
+
+<ul>
+ <li><code><a href="/it/docs/JavaScript/Reference/Statements/var" title="JavaScript/Reference/Statements/var"><code>var</code></a></code></li>
+ <li><code><a href="/it/docs/JavaScript/Reference/Statements/const" title="JavaScript/Reference/Statements/const"><code>const</code></a></code></li>
+ <li><code><a href="/it/docs/JavaScript/New_in_JavaScript/1.7#Block_scope_with_let_(Merge_into_let_Statement)" title="JavaScript/New in JavaScript/1.7#Block scope with let (Merge into let Statement)">New in JavaScript 1.7</a></code></li>
+</ul>