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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
---
title: setter
slug: Web/JavaScript/Reference/Functions/set
tags:
- Funzioni
- JavaScript
- setter
translation_of: Web/JavaScript/Reference/Functions/set
original_slug: Web/JavaScript/Reference/Functions_and_function_scope/set
---
<div>{{jsSidebar("Functions")}}</div>
<p>Il costrutto sintattico <strong><code>set</code></strong> collega una proprietà di un oggetto ad una funzione che viene chiamata quando si ha un tentativo di modifica di quella proprietà.</p>
<h2 id="Sintassi">Sintassi</h2>
<pre class="syntaxbox notranslate">{set <em>prop</em>(<em>val</em>) { . . . }}
{set [expression](<em>val</em>) { . . . }}</pre>
<h3 id="Parametri">Parametri</h3>
<dl>
<dt><code>prop</code></dt>
<dd>Il nome della proprietà da collegare alla funzione data.</dd>
</dl>
<dl>
<dt><code>val</code></dt>
<dd>Un alias per la variabile che contiene il valore che si sta cercando di assegnare a <code>prop</code>.</dd>
<dt>expression</dt>
<dd>A partire da ECMAScript 6, è possibile anche usare espressioni per nomi di proprietà computate da collegare alla funzione data.</dd>
</dl>
<h2 id="Descrizione">Descrizione</h2>
<p>In JavaScript, un setter può essere utilizzato per eseguire una funzione ogniqualvolta una proprietà specificata sta per essere modificata. I setters sono quasi sempre utilizzati insieme ai getters per creare un tipo di pseudo proprietà. Non è possibile avere un setter su una normale proprietà che contiene un valore.</p>
<p>Alcune note da considerare quando si utilizza il costrutto sintattico <code>set</code>:</p>
<div>
<ul>
<li>Può avere un identificatore che può essere od un numero od una stringa;</li>
<li>Deve avere esattamente un parametro(consultare <a class="external" href="http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/" rel="external nofollow">Incompatible <abbr title="ECMAScript 5th edition">ES5</abbr> change: literal getter and setter functions must now have exactly zero or one arguments</a> per maggiori informazioni);</li>
<li>Non può apparire in un letterale di un oggetto con un altro set o con un assegnamento per la stessa proprietà.<br>
( <code>{ set x(v) { }, set x(v) { } }</code> e <code>{ x: ..., set x(v) { } } </code>non sono permessi )</li>
</ul>
</div>
<p>Un setter può essere eliminato usando l'operatore <a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete" title="en-US/docs/JavaScript/Reference/Operators/Special/delete"><code>delete</code></a>.</p>
<h2 id="Esempi">Esempi</h2>
<h3 id="Definire_un_setter_per_nuovi_oggetti_in_inizializzatori_di_oggetti">Definire un setter per nuovi oggetti in inizializzatori di oggetti</h3>
<p>Questo snippet di codice definisce una pseudo proprietà <code>current</code> di un oggetto <code>o </code>che, una volta che vi si assegna un valore, aggiornerà <code>log </code>con quel valore:</p>
<pre class="brush: js notranslate">var o = {
set current (str) {
this.log[this.log.length] = str;
},
log: []
}
</pre>
<p>Nota che <code>current</code> non è definito ed ogni tentativo di accedervi risulterà in un <code>undefined</code>.</p>
<h3 id="Rimuovere_un_setter_con_loperatore_delete">Rimuovere un setter con l'operatore <code>delete</code></h3>
<p>Se vuoi rimuovere il setter usato sopra, puoi semplicemente usare <code>delete</code>:</p>
<pre class="brush: js notranslate">delete o.current;
</pre>
<h3 id="Definire_un_setter_su_oggetti_pre-esistenti_usando_defineProperty">Definire un setter su oggetti pre-esistenti usando <code>defineProperty</code></h3>
<p>Per aggiungere un setter ad un oggetto pre-esistente, usa{{jsxref("Object.defineProperty()")}}.</p>
<pre class="brush: js notranslate">var o = { a:0 };
Object.defineProperty(o, "b", { set: function (x) { this.a = x / 2; } });
o.b = 10; // Runs the setter, which assigns 10 / 2 (5) to the 'a' property
console.log(o.a) // 5</pre>
<h3 id="Usare_il_nome_di_una_proprietà_computata">Usare il nome di una proprietà computata</h3>
<div class="note">
<p><strong>Nota:</strong> Le proprietà computate sono una tecnologia sperimentale<em>,</em> parte dello standard proposto ECMAScript 6, e non sono ancora sufficientemente supportate dai browsers. L'uso di queste proprietà in ambienti che non le supportano produrrà un errore di sintassi.</p>
</div>
<pre class="brush: js notranslate">var expr = "foo";
var obj = {
baz: "bar",
set [expr](v) { this.baz = v; }
};
console.log(obj.baz); // "bar"
obj.foo = "baz"; // run the setter
console.log(obj.baz); // "baz"
</pre>
<h2 id="Specifiche">Specifiche</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specifica</th>
<th scope="col">Status</th>
<th scope="col">Commento</th>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11.1.5', 'Object Initializer')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Definizione iniziale.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-method-definitions', 'Method definitions')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Aggiunti i nomi di proprietà computate.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-method-definitions', 'Method definitions')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Compatibilità_dei_browsers">Compatibilità dei browsers</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Caratteristica</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Supporto base</td>
<td>{{CompatChrome(1)}}</td>
<td>{{ CompatGeckoDesktop("1.8.1") }}</td>
<td>{{ CompatIE(9) }}</td>
<td>9.5</td>
<td>3</td>
</tr>
<tr>
<td>Nomi di proprietà computate</td>
<td>{{CompatNo}}</td>
<td>{{ CompatGeckoDesktop("34") }}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Caratteristica</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Supporto base</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatGeckoMobile("1.8.1") }}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
<tr>
<td>Nomi di proprietà computate</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{ CompatGeckoMobile("34.0") }}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Note_specifiche_per_SpiderMonkey">Note specifiche per SpiderMonkey</h2>
<ul>
<li>A partire da<a href="/en-US/docs/Web/JavaScript/New_in_JavaScript/1.8.1"> JavaScript 1.8.1</a>, i setters non sono più chiamati durante il setting delle properietà negli inizializzatori di oggetti od array.</li>
<li>A partire da SpiderMonkey 38, un setter con {{jsxref("Functions/rest_parameters", "rest parameter", "", 1)}} produce un {{jsxref("SyntaxError")}} come da specifica ES6.</li>
</ul>
<h2 id="Guarda_anche">Guarda anche</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/get">getter</a></li>
<li>{{jsxref("Operators/delete", "delete")}}</li>
<li>{{jsxref("Object.defineProperty()")}}</li>
<li>{{jsxref("Object.defineGetter", "__defineGetter__")}}</li>
<li>{{jsxref("Object.defineSetter", "__defineSetter__")}}</li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters">Defining Getters and Setters</a> nella guida JavaScript</li>
</ul>
|