The WeakMap
object is a collection of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.
new WeakMap([iterable])
iterable
Keys of WeakMaps are of the type Object
only. Primitive data types as keys are not allowed (e.g. a Symbol
can't be a WeakMap
key).
The experienced JavaScript programmer will notice that this API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the four API methods. Such an implementation would have two main inconveniences. The first one is an O(n) search (n being the number of keys in the map). The second one is a memory leak issue. With manually written maps, the array of keys would keep references to key objects, preventing them from being garbage collected. In native WeakMaps, references to key objects are held "weakly", which means that they do not prevent garbage collection in case there would be no other reference to the object.
Because of references being weak, WeakMap
keys are not enumerable (i.e. there is no method giving you a list of the keys). If they were, the list would depend on the state of garbage collection, introducing non-determinism. If you want to have a list of keys, you should use a Map
.
WeakMap.length
length
property is 0.WeakMap.prototype
WeakMap
constructor. Allows the addition of properties to all WeakMap
objects.WeakMap
instancesAll WeakMap
instances inherit from WeakMap.prototype
.
WeakMap.prototype.constructor
WeakMap
function by default.WeakMap.prototype.delete(key)
key
. WeakMap.prototype.has(key)
will return false
afterwards.WeakMap.prototype.get(key)
key
, or undefined
if there is none.WeakMap.prototype.has(key)
key
in the WeakMap
object or not.WeakMap.prototype.set(key, value)
key
in the WeakMap
object. Returns the WeakMap
object.WeakMap.prototype.clear()
WeakMap
object. Note that it is possible to implement a WeakMap
-like object that has a .clear()
method by encapsulating a WeakMap
object that hasn't it (see example on page WeakMap
)WeakMap
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); // a value can be anything, including an object or a function wm2.set(o3, undefined); wm2.set(wm1, wm2); // keys and values can be any objects. Even WeakMaps! wm1.get(o2); // "azerty" wm2.get(o2); // undefined, because there is no key for o2 on wm2 wm2.get(o3); // undefined, because that is the set value wm1.has(o2); // true wm2.has(o2); // false wm2.has(o3); // true (even if the value itself is 'undefined') wm3.set(o1, 37); wm3.get(o1); // 37 wm1.has(o1); // true wm1.delete(o1); wm1.has(o1); // false
WeakMap
-like class with a .clear() methodFor expository purpose, the following example uses the new ECMAScript 2015 class
construct, which is currently not widely implemented.
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 } }
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'WeakMap' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'WeakMap' in that specification. | Draft |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 36 | 12 | 6.0 (6.0) | 11 | 23 | 7.1 |
new WeakMap(iterable) | 38 | 12 | 36 (36) | No support | 25 | No support |
Obsolete clear() method removed | 43 | 12 | 46 (46) | No support | 30 | 9 |
Constructor argument: new WeakMap(null)
| (Yes) | 12 | 37 (37) | 11 | ? | ? |
Monkey-patched set() in constructor | (Yes) | 12 | 37 (37) | ? | ? | ? |
WeakMap() without new throws | (Yes) | 12 | 42 (42) | 11 | ? | ? |
Feature | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 35 | 6.0 (6.0) | No support | No support | 8 |
new WeakMap(iterable) | 38 | 36.0 (36) | No support | No support | No support |
Obsolete clear() method removed | 43 | ? | ? | 30 | 9 |
Constructor argument: new WeakMap(null)
| ? | 37.0 (37) | No support | ? | ? |
Monkey-patched set() in constructor | ? | 37.0 (37) | No support | ? | ? |
WeakMap() without new throws | ? | 42.0 (42) | ? | ? | ? |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap