WeakMap
A WeakMap object holds key-value pairs that do not create strong references to its keys, such that value data can be associated with a key without preventing garbage collection of the key.
Overview
A WeakMap object holds key-value pairs that do not create strong references to its keys, such that value data can be associated with a key without preventing garbage collection of the key.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 36 | 12 | 6 | 8 | 36 | 8 | |
| The WeakMap() constructor creates WeakMap objects. | 36 | 12 | 6 | 8 | 36 | 8 |
WeakMap.WeakMap.iterable allowed `new WeakMap(iterable)` | 38 | 12 | 36 | 9 | 38 | 9 |
WeakMap.WeakMap.null allowed `new WeakMap(null)` | 36 | 12 | 37 | 8 | 36 | 8 |
| The delete() method of WeakMap instances removes the entry specified by the key from this WeakMap. | 36 | 12 | 6 | 8 | 36 | 8 |
| The get() method of WeakMap instances returns the value corresponding to the key in this WeakMap, or undefined if there is none. Object values are returned as the same reference that was originally stored, not as a copy, so mutations to the returned object will be reflected anywhere that reference is held, including inside the WeakMap. | 36 | 12 | 6 | 8 | 36 | 8 |
| The has() method of WeakMap instances returns a boolean indicating whether an entry with the specified key exists in this WeakMap or not. | 36 | 12 | 6 | 8 | 36 | 8 |
| The set() method of WeakMap instances adds a new entry with a specified key and value to this WeakMap, or updates an existing entry if the key already exists. | 36 | 12 | 6 | 8 | 36 | 8 |
WeakMap.symbol as keys Non-registered symbols as keys | 109 | 109 | 146 | 16.4 | 109 | 16.4 |
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. This has been fixed in version 38 and later to return `false` as per the ES2015 standard.
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. However, the ES2015 specification specifies to return `undefined` instead. Furthermore, `WeakMap.prototype.get` accepted an optional second argument as a fallback value, which is not part of the standard. Both non-standard behaviors are removed in version 38 and higher.
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. This has been fixed in version 38 and later to return `false` as per the ES2015 standard.
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. This has been fixed in version 38 and later to return `false` as per the ES2015 standard.
Syntax
const privateData = new WeakMap();
class User {
constructor(name) {
privateData.set(this, { name });
}
getName() {
return privateData.get(this).name;
}
} Live demo
Attach metadata to DOM elements
relatedde-ta.. with DOM objectbody change to, WeakMap.
Check whether an object is processed
efficiency to manage.. with object that processed or or WeakMap.
Use cases
Using WeakMap
A WeakMap object holds key-value pairs that do not create strong references to its keys, such that value data can be associated with a key without preventing garbage collection of the key.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features