diff options
Diffstat (limited to 'files/it/web/javascript/reference/operators/super')
-rw-r--r-- | files/it/web/javascript/reference/operators/super/index.html | 181 |
1 files changed, 0 insertions, 181 deletions
diff --git a/files/it/web/javascript/reference/operators/super/index.html b/files/it/web/javascript/reference/operators/super/index.html deleted file mode 100644 index aee5f694b1..0000000000 --- a/files/it/web/javascript/reference/operators/super/index.html +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: super -slug: Web/JavaScript/Reference/Operators/super -translation_of: Web/JavaScript/Reference/Operators/super ---- -<div>{{jsSidebar("Operators")}}</div> - -<p>La parola chiave <strong>super</strong> viene usata per chiamare le funzioni dell'oggetto padre.</p> - -<p>Il <code>super.prop</code> ed espressioni con <code>super[expr]</code> sono valide in ogni <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions">definizione di metodo</a> sia nelle <a href="/en-US/docs/Web/JavaScript/Reference/Classes">classi</a> e <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">oggetti literals</a>.</p> - -<h2 id="Sintassi">Sintassi</h2> - -<pre class="syntaxbox">super([arguments]); // chiama il costruttore padre. -super.functionOnParent([arguments]); -</pre> - -<h2 id="Descrizione">Descrizione</h2> - -<p>Quando viene usata in un costruttore, la parola chiave <code>super</code> deve essere usata prima della parola chiave <code>this</code>. La parola chiave <code>super</code> può essere usata anche per chiamare funzioni dell'oggetto padre.</p> - -<h2 id="Esempio">Esempio</h2> - -<h3 id="Usare_super_nelle_classi">Usare <code>super</code> nelle classi</h3> - -<p>Questo pezzo di codice è preso da <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">classes sample</a> (<a href="https://googlechrome.github.io/samples/classes-es6/index.html">live demo</a>). <code>super</code> è chiamato per evitare la duplicazione del codice comune ad entrambi i costruttori <code>Rectangle</code> e <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">Square</span></font>.</p> - -<pre class="brush: js">class Polygon { - constructor(height, width) { - this.name = 'Polygon'; - this.height = height; - this.width = width; - } - sayName() { - console.log('Hi, I am a ', this.name + '.'); - } -} - -class Square extends Polygon { - constructor(length) { - this.height; // ReferenceError, super deve essere chiamato per primo! - - // Chiama il costruttore della classe padre - super(length, length); - - // Nota: Nelle classi derivate super() deve essere chiamato prima - // dell'uso di 'this'. - this.name = 'Square'; - } - - get area() { - return this.height * this.width; - } - - set area(value) { - this.area = value; - } -}</pre> - -<h3 id="Usare_super_con_metodi_statici">Usare <code>super</code> con metodi statici</h3> - -<p>Puoi usare super anche chiamare metodi <a href="/en-US/docs/Web/JavaScript/Reference/Classes/static">statici</a>.</p> - -<pre class="brush: js"><code>class Rectangle { - constructor() {} - static logNbSides() { - return 'I have 4 sides'; - } -} - -class Square extends Rectangle { - constructor() {} - static logDescription() { - return super.logNbSides() + ' which are all equal'; - } -} -Square.logDescription(); // 'I have 4 sides which are all equal'</code></pre> - -<h3 id="Cancellare_una_proprietà_del_super_causa_eccezione">Cancellare una proprietà del super causa eccezione</h3> - -<p>Non puoi cancellare una proprietà della classe padre usando l' <a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete">operatore delete</a> e <code>super.prop</code> o <code>super[esperssione]</code>, questo causerà un {{jsxref("ReferenceError")}}.</p> - -<pre class="brush: js">class Base { - constructor() {} - foo() {} -} -class Derived extends Base { - constructor() {} - delete() { - delete super.foo; - } -} - -new Derived().delete(); // ReferenceError: uso della delete con 'super'. </pre> - -<h3 id="Super.prop_non_può_sovrascrivere_proprietà_non_scrivibili"><code>Super.prop</code> non può sovrascrivere proprietà non scrivibili</h3> - -<p>Quando si definisce una proprietà non riscrivibile con ad esempio {{jsxref("Object.defineProperty")}}, <code>super</code> non può modificarne il valore.</p> - -<pre class="brush: js"><code>class X { - constructor() { - Object.defineProperty(this, 'prop', { - configurable: true, - writable: false, - value: 1 - }); - } -} - -class Y extends X { - constructor() { - super(); - } - foo() { - super.prop = 2; // Non posso sovrascrivere il valore. - } -} - -var y = new Y(); -y.foo(); // TypeError: "prop" is read-only -console.log(y.prop); // 1</code></pre> - -<h3 id="Uso_di_super.prop_in_oggetti_literals">Uso di <code>super.prop</code> in oggetti literals</h3> - -<p>Super può essere utilizzato anche nella <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">initializzazione di oggetti</a> con notazione letterale. In questo esempio, due oggetti definiscono un metodo. Nel secondo oggetto, <code>super</code> chiama il metodo del primo oggetto. Questo funziona grazie all'aiuto di {{jsxref("Object.setPrototypeOf()")}} con cui siamo in grado di impostare il prototipo di <code>obj2</code> con l'oggetto <code>obj1</code>, in modo che <code>super</code> sia in grado di trovare <code>method1</code> in <code>obj1</code>.</p> - -<pre class="brush: js">var obj1 = { - method1() { - console.log("method 1"); - } -} - -var obj2 = { - method2() { - super.method1(); - } -} - -Object.setPrototypeOf(obj2, obj1); -obj2.method2(); // logs "method 1" -</pre> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('ES6', '#sec-super-keyword', 'super')}}</td> - <td>{{Spec2('ES6')}}</td> - <td>Initial definition.</td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-super-keyword', 'super')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td></td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibili">Browser compatibili</h2> - -<p>{{Compat("javascript.operators.super")}}</p> - -<div id="compat-desktop"></div> - -<h2 id="Gecko_specific_notes">Gecko specific notes</h2> - -<ul> - <li><code>super()</code> non funziona come previsto in prototipi built-in.</li> -</ul> - -<h2 id="Vedi_anche">Vedi anche</h2> - -<ul> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classi</a></li> -</ul> |