1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
---
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>
|