JavaScript WeakRef Object
A WeakRef holds a weak reference to an object, which doesn't stop it from being garbage-collected. It is an advanced tool for caches; most code never needs it.
Description
A WeakRef object holds a weak reference to another object, its target. Unlike a normal reference, it doesn't keep the target alive: if nothing else refers to it, the garbage collector may remove it, after which deref() returns undefined.
When and whether garbage collection happens is unpredictable and differs between browsers, so code must never depend on it. WeakRefs are mainly useful for caches of large objects that can be recreated if they disappear. For associating data with objects, WeakMap is simpler and usually better.
Calling deref() returns the target if it still exists, or undefined if it has been collected, so always check the result before using it. Within a single synchronous piece of code, a target that was alive at the start stays alive until it finishes. The JavaScript specification deliberately leaves the details of garbage collection to each engine, so code that works in one browser may behave differently in another.
Creating
new WeakRef(target)Example
Browser Support
The following information will show you the current browser support for the WeakRef object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





