aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/global_objects/weakmap/index.html
blob: 27fdd6a71d325c779d8194a51468e5f67c13da76 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
---
title: WeakMap
slug: Web/JavaScript/Reference/Global_Objects/WeakMap
tags:
  - ECMAScript6
  - Experimental
  - JavaScript
  - NeedsTranslation
  - TopicStub
  - WeakMap
translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap
---
<div>{{JSRef}}</div>

<p>L'objecte <strong><code>WeakMap</code></strong> és una col·lecció de parelles clau/valor on les claus són dèbilment referenciades. Les claus han de ser objectes i els valors poden ser valors aribitraris.</p>

<h2 id="Sintaxi">Sintaxi</h2>

<pre class="syntaxbox notranslate"><code>new WeakMap([iterable])
</code></pre>

<h3 id="Paràmetres">Paràmetres</h3>

<dl>
 <dt><code>iterable</code></dt>
 <dd>Iterable és un Array o un altre objecte iterable el qual els seus elements són parelles clau/valor (Array de 2 elements). Cada parella clau/valor serà afegida al nou WeakMap. null és tractat com a undefined.</dd>
</dl>

<h2 id="Descripció">Descripció</h2>

<p>Les claus de WeakMaps són només de tipus <code>Object.</code> {{Glossary("Primitive", "Primitive data types")}} com a claus no són permesos (e.g. a {{jsxref("Symbol")}} no pot ser una clau <code>WeakMap</code>).</p>

<p>La clau d'un WeakMap es sostè dèbilment.  El que significa que, si no hi ha altres referències fortes a la clau, llavors la entrada sencera serà eliminada del WeakMap pel recol·lector de brossa (garbage collector).</p>

<h3 id="Per_què_WeakMap">Per què <em>Weak</em>Map?</h3>

<p>El programador expert en JavaScript s'adonarà que aquesta API es podria implementar en JavaScript amb dos arrays (una per a claus, i una per valors) compartides pels quatre mètodes de l'API. Tal implementació tindria dos inconvenients principals. El primer és que la cerca té un cost de O(n) (on n és el nombre de claus al mapa). El segon és que té problemes de fuita de memòria (memory leak). Amb els mapes mantinguts manualment, l'array de claus mantindria referències a le objectes clau, evitant que aquests fóssin eliminats de memòria pel recol·lector de brossa. Als WeakMaps natius, les referències als objectes clau són "dèbils", que vol dir que el recol·lector de brossa pot eliminar l'objecte de memòria si aquest només és referenciat per referències dèbils.</p>

<p>Degut a que les referències són dèbils les claus del WeakMap no són enumerables (és a dir, no hi ha cap mètode que us retornarà un llistat de claus). Si aquest mètode existís, aquest dependria de l'estat del recol·lector de brossa, introduïnt un comportament no determinista. Si voleu tenir un llistat amb les claus, l'haureu de mantenir pel vostre compte.</p>

<h2 id="Propietats">Propietats</h2>

<dl>
 <dt><code>WeakMap.length</code></dt>
 <dd>El valor de la propietat <code>length</code> és 0.</dd>
 <dt>{{jsxref("WeakMap.prototype")}}</dt>
 <dd>Representa el prototip pel constructor <code>WeakMap.</code> Permet l'adició de propietats a tots els objectes <code>WeakMap.</code></dd>
</dl>

<h2 id="Instàncies_WeakMap">Instàncies <code>WeakMap</code></h2>

<p>Totes les instàncies <code>WeakMap</code> hereten de {{jsxref("WeakMap.prototype")}}.</p>

<h3 id="Propietats_2">Propietats</h3>

<p>{{page('ca/Web/JavaScript/Reference/Global_Objects/WeakMap/prototype','Properties')}}</p>

<h3 id="Mètodes">Mètodes</h3>

<p>{{page('ca/Web/JavaScript/Reference/Global_Objects/WeakMap/prototype','Methods')}}</p>

<h2 id="Exemples">Exemples</h2>

<h3 id="Utilitzar_WeakMap">Utilitzar <code>WeakMap</code></h3>

<pre class="brush: js notranslate">var wm1 = new WeakMap(),
    wm2 = new WeakMap(),
    wm3 = new WeakMap();
var o1 = {},
    o2 = function(){},
    o3 = window;

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // un valor pot ser qualsevol cosa, incloent un objecte object o una funció
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // claus i valors poden ser qualsevol objecte. Fins i tot WeakMaps!

wm1.get(o2); // "azerty"
wm2.get(o2); // undefined, ja que no hi ha cap valor per o2 a wm2
wm2.get(o3); // undefined, al tenir assignat aquest valor

wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true (encara que el valor en si sigui 'undefined')

wm3.set(o1, 37);
wm3.get(o1); // 37

wm1.has(o1); // true
wm1.delete(o1);
wm1.has(o1); // false
</pre>

<h3 id="Implementar_una_classe_tipus_WeakMap_amb_un_mètode_.clear">Implementar una classe tipus <code>WeakMap</code> amb un mètode .clear()</h3>

<p>Amb fins expositius, l'exemple següent utilitza el nou constructor <code>class</code> d' ECMAScript 6, el qual no està àmpliament implementat.</p>

<pre class="brush: js notranslate">class ClearableWeakMap {
  constructor(init) {
    this._wm = new WeakMap(init)
  }
  clear() {
    this._wm = new WeakMap()
  }
  delete(k) {
    return this._wm.delete(k)
  }
  get(k) {
    return this._wm.get(k)
  }
  has(k) {
    return this._wm.has(k)
  }
  set(k, v) {
    this._wm.set(k, v)
    return this
  }
}
</pre>

<h2 id="Especificacions">Especificacions</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Especificació</th>
   <th scope="col">Estat</th>
   <th scope="col">Comentaris</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ES6', '#sec-weakmap-objects', 'WeakMap')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Definició inicial.</td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Suport bàsic</td>
   <td>36</td>
   <td>{{CompatGeckoDesktop("6.0")}}</td>
   <td>11</td>
   <td>{{ CompatOpera(23) }}</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>new WeakMap(iterable)</code></td>
   <td>38</td>
   <td>{{CompatGeckoDesktop("36")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatOpera(25) }}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>clear()</code></td>
   <td>36</td>
   <td>{{CompatGeckoDesktop("20.0")}}</td>
   <td>11</td>
   <td>{{ CompatOpera(23) }}</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new WeakMap(null)</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>set()</code> in constructor</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>WeakMap()</code> without <code>new</code> throws</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</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>Suport bàsic</td>
   <td>35</td>
   <td>{{CompatGeckoMobile("6.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>new WeakMap(iterable)</code></td>
   <td>38</td>
   <td>{{CompatGeckoMobile("36")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>clear()</code></td>
   <td>35</td>
   <td>{{CompatGeckoMobile("20.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new WeakMap(null)</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>set()</code> in constructor</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>WeakMap()</code> without <code>new</code> throws</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Vegeu_també">Vegeu també</h2>

<ul>
 <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=547941">Bug de WeakMap a Mozilla</a></li>
 <li><a href="http://fitzgeraldnick.com/weblog/53/">Amagant detalls d'implementació amb els WeakMaps de l'ECMAScript 6</a></li>
 <li>{{jsxref("Map")}}</li>
 <li>{{jsxref("Set")}}</li>
 <li>{{jsxref("WeakSet")}}</li>
</ul>