WeakMap() constructor

The WeakMap() constructor creates WeakMap objects which are a collections of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.

You can learn more about WeakMaps in the section WeakMap object in Keyed collections.

Syntax

new WeakMap([iterable])

Parameters

iterable
Iterable is an Array or other iterable object whose elements are key-value pairs (2-element Arrays). Each key-value pair will be added to the new WeakMap. null is treated as undefined.

Examples

Using WeakMap

const wm1 = new WeakMap(),
      wm2 = new WeakMap(),
      wm3 = new WeakMap();
const 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

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'WeakMap constructor' in that specification.

Browser compatibility

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
WeakMap() constructorChrome Full support 36Edge Full support 12Firefox Full support 6IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 6Opera Android Full support 24Safari iOS Full support 8Samsung Internet Android Full support 3.0nodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
new WeakMap(iterable)Chrome Full support 38Edge Full support 12Firefox Full support 36IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 36Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
WeakMap() without new throwsChrome Full support 36Edge Full support 12Firefox Full support 42IE Full support 11Opera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 42Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
new WeakMap(null)Chrome Full support 36Edge Full support 12Firefox Full support 37IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 37Opera Android Full support 24Safari iOS Full support 8Samsung Internet Android Full support 3.0nodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.

Legend

Full support
Full support
No support
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

See also