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
|
---
title: Object.freeze()
slug: Web/JavaScript/Reference/Global_Objects/Object/freeze
translation_of: Web/JavaScript/Reference/Global_Objects/Object/freeze
---
<div>{{JSRef}}</div>
<p>El mètode <code><strong>Object.freeze()</strong></code> congela un objecte: és a dir, evita que se li puguin afegir noves propietats, eliminar propietats ja existents així com modificar els paràmetres d'enumerabilitat, configurabilitat i possibilitat d'escriptura de les propietats existents. Això, en essència fa que l'objecte esdevingui immutable a efectes pràctics. El mètode retorna l'objecte que s'ha congelat.</p>
<h2 id="Sintaxi">Sintaxi</h2>
<pre class="syntaxbox"><code>Object.freeze(<var>obj</var>)</code></pre>
<h3 id="Paràmetres">Paràmetres</h3>
<dl>
<dt><code>obj</code></dt>
<dd>L'objecte a congelar.</dd>
</dl>
<h2 id="Descripció">Descripció</h2>
<p>Res pot ser afegit o eliminat del conjunt de propietats d'un objecte congelat. Qualsevol intent fallarà o bé sense reportar cap error o bé llençant una excepció {{jsxref("TypeError")}} (és l'excepció més freqüent que serà llençada però en pot llençat altres tipus; a l'utilitzar el {{jsxref("Strict_mode", "mode estricte", "", 1)}}).</p>
<p>Congelar un objecte també evita que es puguin modificar els valors de les seves propietats. Les propietats d'accés (getters i setters) segueixen funcionan (donant la il·lusió que s'ha canviat el valor). Cal advertir, però, que els valors que siguin objectes sí que es poden modificar, a no ser que aquests objectes també estiguin congelats.</p>
<h2 id="Exemples">Exemples</h2>
<pre class="brush: js">var obj = {
prop: function() {},
foo: 'bar'
};
// Es poden afegir noves propietats, així com canviar o eliminar les propietats ja existents
obj.foo = 'baz';
obj.lumpy = 'woof';
delete obj.prop;
var o = Object.freeze(obj);
assert(Object.isFrozen(obj) === true);
// A partir d'ara qualsevol canvi fallarà
obj.foo = 'quux'; // falla silenciosament
obj.quaxxor = 'the friendly duck'; // romàn en silenci i no afegeix la propietat
// ...i en mode estricte qualsevol intent llençarà una excepció TypeError
function fail(){
'use strict';
obj.foo = 'sparky'; // llença TypeError
delete obj.quaxxor; // llença TypeError
obj.sparky = 'arf'; // llença TypeError
}
fail();
// Intentar realitzar canvis a través de Object.defineProperty també resultaran en excepcions
Object.defineProperty(obj, 'ohai', { value: 17 }); // llença TypeError
Object.defineProperty(obj, 'foo', { value: 'eit' }); // llença TypeError
</pre>
<p>L'exemple següent demostra que valors de tipus objecte pertanyents a propietats d'un objecte congelat sí que es poden modificar.(<code>freeze</code> no s'aplica de manera recursiva).</p>
<pre class="brush: js">obj1 = {
internal: {}
};
Object.freeze(obj1);
obj1.internal.a = 'aValue';
obj1.internal.a // 'aValue'
// Per a fer que obj sigui totalment immutable cal congelar tots els objectes referenciats per aquest.
// Per a aconseguir això utilitzem la funció següent
function deepFreeze(obj) {
// Obté els nomes de les propietats definides a l'objecte obj
var propNames = Object.getOwnPropertyNames(obj);
// Congela les propietats abans de congelar l'objecte en si
propNames.forEach(function(name) {
var prop = obj[name];
// Congela prop si aquest és un objecte
if (typeof prop == 'object' && !Object.isFrozen(prop))
deepFreeze(prop);
});
// Congela l'objecte pare
return Object.freeze(obj);
}
obj2 = {
internal: {}
};
deepFreeze(obj2);
obj2.internal.a = 'anotherValue';
obj2.internal.a; // undefined
</pre>
<h2 id="Notes">Notes</h2>
<p>A l'EcmaScript 5, si l'argument passat a aquest mètode no és un objecte (un valor primitiu), llençarà un {{jsxref("TypeError")}}. A l'EcmaScript 6, un argument que no sigui un objecte serà tractat com si fós un objecte congelat ordinari, i simplement el retornarà.</p>
<pre class="brush: js">> Object.freeze(1)
TypeError: 1 no és un objecte // Codi EcmaScript 5
> Object.freeze(1)
1 // Codi EcmaScript 6
</pre>
<h2 id="Especificacions">Especificacions</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificacions</th>
<th scope="col">Estat</th>
<th scope="col">Comentaris</th>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.3.9', 'Object.freeze')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Definició inicial. Implementat a JavaScript 1.8.5.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.freeze', 'Object.freeze')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Firefox (Gecko)</th>
<th>Chrome</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Suport bàsic</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>Característica</th>
<th>Firefox Mobile (Gecko)</th>
<th>Android</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Vegeu_també">Vegeu també</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>
|