WeakMap()-Konstruktor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Der WeakMap()
-Konstruktor erstellt WeakMap
-Objekte.
Syntax
Parameter
iterable
-
Ein
Array
oder ein anderes iterierbares Objekt, das ein zwei-elementiges array-ähnliches Objekt produziert, dessen erstes Element ein Wert ist, der alsWeakMap
-Schlüssel verwendet wird und dessen zweites Element der Wert ist, der diesem Schlüssel zugeordnet wird. Jedes Schlüssel-Wert-Paar wird der neuenWeakMap
hinzugefügt.null
wird alsundefined
behandelt.
Beispiele
Verwendung von WeakMap
js
const wm1 = new WeakMap();
const wm2 = new WeakMap();
const wm3 = new WeakMap();
const o1 = {};
const o2 = () => {};
const 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
Spezifikationen
Specification |
---|
ECMAScript® 2026 Language Specification # sec-weakmap-constructor |