Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WeakMap() constructor

BaselineWidely available

TheWeakMap() constructor createsWeakMap objects.

Syntax

js
new WeakMap()new WeakMap(iterable)

Note:WeakMap() can only be constructed withnew. Attempting to call it withoutnew throws aTypeError.

Parameters

iterable

AnArray or otheriterable object that produces a two-element array-like object whose first element is a value that will be used as aWeakMap key and whose second element is the value to associate with that key. Each key-value pair will be added to the newWeakMap.null is treated asundefined.

Examples

Using 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 functionwm2.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 wm2wm2.get(o3); // undefined, because that is the set valuewm1.has(o2); // truewm2.has(o2); // falsewm2.has(o3); // true (even if the value itself is 'undefined')wm3.set(o1, 37);wm3.get(o1); // 37wm1.has(o1); // truewm1.delete(o1);wm1.has(o1); // false

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-weakmap-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp