aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/global_objects/object/freeze/index.html
blob: 26201fdb0cc5c83f115c34062f3c0a35fd315d1d (plain)
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
---
title: Object.freeze()
slug: Web/JavaScript/Reference/Global_Objects/Object/freeze
translation_of: Web/JavaScript/Reference/Global_Objects/Object/freeze
---
<div>{{JSRef}}</div>

<p>Il metodo <code><strong>Object.freeze()</strong></code> congela un oggetto: ne previene l'aggiunta, la modifica e la rimozione di proprietà, inclusa la loro enumerabilità, configurabilità e accessibilità. In sostanza, l'oggetto è reso effettivamente immutabile. Il metodo restituisce lo stesso oggetto che è stato passato alla funzione. </p>

<h2 id="Sintassi">Sintassi</h2>

<pre class="syntaxbox"><code>Object.freeze(<var>obj</var>)</code></pre>

<h3 id="Parametri">Parametri</h3>

<dl>
 <dt><code>obj</code></dt>
 <dd>L'oggetto da congelare.</dd>
</dl>

<h3 id="Valore_di_ritorno">Valore di ritorno</h3>

<p>L'oggetto passato alla funzione.</p>

<h2 id="Descrizione">Descrizione</h2>

<p>Nulla può essere aggiunto o rimosso dall'insieme delle proprietà di un oggetto congelato. Qualsiasi tentativo di fare ciò fallirebbe, o silenziosamente o attraverso il ritorno di un errore {{jsxref("TypeError")}} (più frequentemente, ma non necessariamente, quest'ultimo scenario accadrebbe in {{jsxref("Strict_mode", "strict mode", "", 1)}}).</p>

<p>I valori delle proprietà non possono essere cambiati, anche quando si tratta di setters e getters. Da notare che se un oggetto costituisce il valore di una proprietà, esso può essere ancora modificato senza problemi, a meno che anch'esso non sia stato congelato.</p>

<h2 id="Esempi">Esempi</h2>

<pre class="brush: js">var obj = {
  prop: function() {},
  foo: 'bar'
};

// Nuove proprietà possono essere aggiunte, proprietà già esistenti possono
// essere modificate o rimosse
obj.foo = 'baz';
obj.lumpy = 'woof';
delete obj.prop;


// Sia l'oggetto che viene passato che quello restituito verranno congelati.
// No serve salvare l'oggetto restituito per congelare l'originale
var o = Object.freeze(obj);

o === obj; // true
Object.isFrozen(obj); // === true

// Adesso qualsiasi cambiamento fallirà
obj.foo = 'quux'; // silenziosamente, non succede niente
obj.quaxxor = 'the friendly duck'; // silenziosamente, non aggiungerà alcuna proprietò


// ...e nella modalità strict questi tentativi di modifica faranno lanciare TypeErrors
function fail(){
  'use strict';
  obj.foo = 'sparky'; // throws a TypeError
  delete obj.quaxxor; // throws a TypeError
  obj.sparky = 'arf'; // throws a TypeError
}

fail();


// Tentare di cambiare attraverso Object.defineProperty farà anche lanciare un TypeError
Object.defineProperty(obj, 'ohai', { value: 17 }); // throws a TypeError
Object.defineProperty(obj, 'foo', { value: 'eit' }); // throws a TypeError
</pre>

<p>Il seguente esempio mostra come oggetti che sono valori di proprietà possono essere mutati(il congelamento si ferma ad un solo livello di profondità).</p>

<pre class="brush: js">obj1 = {
  internal: {}
};

Object.freeze(obj1);
obj1.internal.a = 'aValue';

obj1.internal.a // 'aValue'


// Per fare un oggetto totalmente non modificabile, congela ciascun oggetto in obj.
// Per farlo noi usiamo questa funzione.
function deepFreeze(obj) {

  // Prende tutti i nomi delle proprietà definite in obj
  var propNames = Object.getOwnPropertyNames(obj);

  // Congela tutte le proprietà prima di congelare obj
  propNames.forEach(function(name) {
    var prop = obj[name];

    // Congela prop se esso è un oggetto
    if (typeof prop == 'object' &amp;&amp; prop !== null)
      deepFreeze(prop);
  });

  // Congela se stesso (niente operazione se esso è già congelato)
  return Object.freeze(obj);
}

obj2 = {
  internal: {}
};

deepFreeze(obj2);
obj2.internal.a = 'anotherValue';
obj2.internal.a; // undefined
</pre>

<h2 id="Note">Note</h2>

<p>In ES5, se l'argomento di questo metodo non è un oggetto, allora verrà ritornato un errore {{jsxref("TypeError")}}. In ES2015, un argomento che non è un oggetto verrà trattato come se fosse un normale oggetto già congelato, e verrà perciò semplicemente ritornato.</p>

<pre class="brush: js">&gt; Object.freeze(1)
TypeError: 1 is not an object // ES5 code

&gt; Object.freeze(1)
1                             // ES2015 code
</pre>

<h2 id="Specifiche">Specifiche</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specifica</th>
   <th scope="col">Stato</th>
   <th scope="col">Commento</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.3.9', 'Object.freeze')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Commento iniziale. Implementato in JavaScript 1.8.5.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-object.freeze', 'Object.freeze')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object.freeze', 'Object.freeze')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Funzionalità</th>
   <th>Firefox (Gecko)</th>
   <th>Chrome</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Funzionalità di base</td>
   <td>{{CompatGeckoDesktop("2")}}</td>
   <td>{{CompatChrome("6")}}</td>
   <td>{{CompatIE("9")}}</td>
   <td>{{CompatOpera("12")}}</td>
   <td>{{CompatSafari("5.1")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Funzionalità</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Android</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Funzionalità di base</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Guarda_anche">Guarda anche</h2>

<ul>
 <li>{{jsxref("Object.isFrozen()")}}</li>
 <li>{{jsxref("Object.preventExtensions()")}}</li>
 <li>{{jsxref("Object.isExtensible()")}}</li>
 <li>{{jsxref("Object.seal()")}}</li>
 <li>{{jsxref("Object.isSealed()")}}</li>
</ul>