The WeakRef() constructor creates WeakRef objects.
WeakRef() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
Syntax
Parameters
target-
The target value the WeakRef should refer to (also called the referent). Must be an object or a non-registered symbol.
Return value
A new WeakRef object referring to the given target value.
Exceptions
TypeError-
Thrown if
targetis not an object or a non-registered symbol.
Examples
Creating a new WeakRef object
See the main WeakRef page for a complete example.
class Counter {
constructor(element) {
// Remember a weak reference to a DOM element
this.ref = new WeakRef(element);
this.start();
}
}
Specifications
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | |
WeakRef |
84 | 84 | 79 | 70 | 14.1 | 84 | 79 | 60 | 14.5 | 14.0 | 84 | 1.0 | 14.6.0 |
See also
© 2005–2024 MDN 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/WeakRef/WeakRef