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/statements/function_star_ | |
| 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/statements/function_star_')
| -rw-r--r-- | files/it/web/javascript/reference/statements/function_star_/index.html | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/statements/function_star_/index.html b/files/it/web/javascript/reference/statements/function_star_/index.html new file mode 100644 index 0000000000..a71ccfc55e --- /dev/null +++ b/files/it/web/javascript/reference/statements/function_star_/index.html @@ -0,0 +1,280 @@ +--- +title: function* +slug: Web/JavaScript/Reference/Statements/function* +translation_of: Web/JavaScript/Reference/Statements/function* +--- +<div>{{jsSidebar("Statements")}}</div> + +<p>La dichiarazione <code><strong>function*</strong></code> (la parola chiave <code>function</code> seguita da un asterisco) definisce una <em>funzione generatrice</em>, la quale restituisce un oggetto di tipo {{jsxref("Global_Objects/Generator","Generator")}}.</p> + +<p>{{EmbedInteractiveExample("pages/js/statement-functionasterisk.html")}}</p> + +<div class="noinclude"> +<p>È anche possibile definire una funzione generatrice usando il costrutto {{jsxref("GeneratorFunction")}} e una {{jsxref("Operators/function*", "espressione function*")}}.</p> +</div> + +<h2 id="Sintassi">Sintassi</h2> + +<pre class="syntaxbox">function* <em>nome</em>([<em>param</em>[, <em>param</em>[, ... <em>param</em>]]]) { + <em>istruzioni</em> +} +</pre> + +<dl> + <dt><code>nome</code></dt> + <dd>Il nome della funzione.</dd> +</dl> + +<dl> + <dt><code>param</code></dt> + <dd>Gli argomenti passati alla funzione. Una funzione può avere fino a 255 argomenti.</dd> +</dl> + +<dl> + <dt><code>istruzioni</code></dt> + <dd>Le istruzioni che compongono il corpo della funzione.</dd> +</dl> + +<h2 id="Descrizione">Descrizione</h2> + +<p>I generatori sono funzioni dalle quali è possibile uscire e poi rientrarvi in un secondo momento. Il loro contesto (binding delle variabili) verrà salvato all'uscita per quando vi entrerà successivamente.</p> + +<p>La chiamata ad un generatore non viene eseguita immediatamente; la funzione ritornerà invece un oggetto <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterator">iterator</a>. Quando il metodo <code>next()</code> dell'iteratore viene chiamato, il corpo del generatore viene eseguito fino alla prima espressione {{jsxref("Operators/yield", "yield")}}, la quale specifica quale espressione ritornare dall'iteratore oppure, con l'espressione {{jsxref("Operators/yield*", "yield*")}}, delegare questo valore ad un'altra funzione generatrice. Il metodo <code>next()</code> restituisce un oggetto con proprietà <code>value</code> contenente il valore da restituito all'iteratore ed una proprietà <code>done</code> che contiene un valore di tipo boolean per indicare se il generatore ha restituito l'ultimo valore. Chiamando il metodo <code>next()</code> con un argomento farà riprendere l'esecuzione della funzione generatrice, sostituendo l'istruzione <code>yield</code> in cui l'esecuzione era stata fermata con l'argomento della funzione <code>next()</code>. </p> + +<h2 id="Esempi">Esempi</h2> + +<h3 id="Esempio_semplice">Esempio semplice</h3> + +<pre class="brush: js">function* idMaker(){ + var index = 0; + while(index < 3) + yield index++; +} + +var gen = idMaker(); + +console.log(gen.next().value); // 0 +console.log(gen.next().value); // 1 +console.log(gen.next().value); // 2 +console.log(gen.next().value); // undefined +// ...</pre> + +<h3 id="Esempio_con_yield*">Esempio con yield*</h3> + +<pre class="brush: js">function* anotherGenerator(i) { + yield i + 1; + yield i + 2; + yield i + 3; +} + +function* generator(i){ + yield i; + yield* anotherGenerator(i); + yield i + 10; +} + +var gen = generator(10); + +console.log(gen.next().value); // 10 +console.log(gen.next().value); // 11 +console.log(gen.next().value); // 12 +console.log(gen.next().value); // 13 +console.log(gen.next().value); // 20 +</pre> + +<h3 id="Passare_argomenti_ai_Generatori">Passare argomenti ai Generatori</h3> + +<pre class="brush: js">function* logGenerator() { + console.log(yield); + console.log(yield); + console.log(yield); +} + +var gen = logGenerator(); + +// the first call of next executes from the start of the function +// until the first yield statement +gen.next(); +gen.next('pretzel'); // pretzel +gen.next('california'); // california +gen.next('mayonnaise'); // mayonnaise +</pre> + +<h3 id="I_generatori_non_sono_costruttori">I generatori non sono costruttori</h3> + +<pre class="brush: js example-bad">function* f() {} +var obj = new f; // solleva "TypeError: f is not a constructor"</pre> + +<h2 id="Specifiche">Specifiche</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specifica</th> + <th scope="col">Stato</th> + <th scope="col">Commenti</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ES6', '#', 'function*')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definizione iniziale</td> + </tr> + <tr> + <td>{{SpecName('ES7', '#', 'function*')}}</td> + <td>{{Spec2('ES7')}}</td> + <td>I generatori non devono avere essere usati come costruttori e deveo lanciare un eccezione quando vengono usati con la parola chiave <code>new</code>.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#', 'function*')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilità_per_i_browser">Compatibilità per i browser</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th> Edge</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Supporto base</td> + <td>{{CompatChrome(39.0)}}</td> + <td>{{CompatGeckoDesktop("26.0")}}</td> + <td>{{CompatNo}}</td> + <td>13</td> + <td>26</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>yield*</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("27.0")}}</td> + <td>{{CompatNo}}</td> + <td>13</td> + <td>26</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>IteratorResult</code> invece delle eccezioni</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("29.0")}}</td> + <td>{{CompatNo}}</td> + <td>13</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>Non istanziabile con <code>new</code> {ES2016)</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("43.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Supporto base</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("26.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(39.0)}}</td> + </tr> + <tr> + <td><code>yield*</code></td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("27.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td><code>IteratorResult</code> invece delle eccezioni</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("29.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Non istanziabile con <code>new</code> (ES2016)</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("43.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Note_specifiche_per_Firefox">Note specifiche per Firefox</h2> + +<h4 id="Generatori_e_iteratori_in_Firefox_nelle_versioni_precedenti_alla_26">Generatori e iteratori in Firefox nelle versioni precedenti alla 26</h4> + +<p>Versioni di FIrefox precedenti implementano una versione più vecchia della proposta di standard per i generatori. Tra le varie differenze, in queste versioni i generatori erano definiti usando la parola chiave <code>function</code> (senza asterisco) per le funzioni semplici. Per maggiori informazioni fare riferimento a {{jsxref("Statements/Legacy_generator_function", "funzioni generatrici (legacy)")}}.</p> + +<h4 id="IteratorResult_al_posto_delle_eccezioni"><code>IteratorResult</code> al posto delle eccezioni</h4> + +<p>A partire da Gecko 29 {{geckoRelease(29)}}, la funzione generatrice completata non solleva più eccezioni {{jsxref("TypeError")}} "generator has already finished". Restituisce invece oggetti di tipo <code>IteratorResult</code> come il seguente <code>{ value: undefined, done: true }</code> ({{bug(958951)}}).</p> + +<h2 id="Vedi_anche">Vedi anche</h2> + +<ul> + <li>{{jsxref("Operators/function*", "Espressione function*")}}</li> + <li>Oggetto {{jsxref("GeneratorFunction")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">The Iterator protocol</a></li> + <li>{{jsxref("Operators/yield", "yield")}}</li> + <li>{{jsxref("Operators/yield*", "yield*")}}</li> + <li>Oggetto {{jsxref("Function")}}</li> + <li>{{jsxref("Statements/function", "dichiarazione di funzioni")}}</li> + <li>{{jsxref("Operators/function", "espressione function")}}</li> + <li>{{jsxref("Functions_and_function_scope", "Funzioni e scope di funzioni")}}</li> + <li>Altre risorse sul web: + <ul> + <li><a href="http://facebook.github.io/regenerator/">Regenerator</a> un compilatore di generatori da ES5 a ES5 [Inglese]</li> + <li><a href="http://www.youtube.com/watch?v=qbKWsbJ76-s">Forbes Lindesay: Promises and Generators: control flow utopia -- JSConf EU 2013 </a>[Inglese]</li> + <li><a href="https://www.youtube.com/watch?v=ZrgEZykBHVo&list=PLuoyIZT5fPlG44bPq50Wgh0INxykdrYX7&index=1">Hemanth.HM: The New gen of *gen(){} </a>[Inglese]</li> + <li><a href="http://taskjs.org/">Task.js </a>[Inglese]</li> + </ul> + </li> +</ul> |
