WeakRef.prototype.deref()

The deref method returns the WeakRef instance's target object, or undefined if the target object has been garbage-collected.

Syntax

obj = ref.deref();

Return value

The target object of the WeakRef, or undefined if the object has been garbage-collected.

Notes

See the Notes on WeakRefs section of the WeakRef page for some important notes.

Examples

Using deref

See the Examples section of the WeakRef page for the complete example.

const tick = () => {
  // Get the element from the weak reference, if it still exists
  const element = this.ref.deref();
  if (element) {
    element.textContent = ++this.count;
  } else {
    // The element doesn't exist anymore
    console.log("The element is gone.");
    this.stop();
    this.ref = null;
  }
};

Specifications

Specification
WeakRefs
The definition of 'WeakRef.prototype.deref()' in that specification.

Browser compatibility

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
derefChrome Full support 84Edge Full support 84Firefox Full support 79IE No support NoOpera No support NoSafari No support NoWebView Android Full support 84Chrome Android Full support 84Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs Full support 13.0.0
Disabled
Full support 13.0.0
Disabled
Disabled From version 13.0.0: this feature is behind the --harmony-weak-refs runtime flag.

Legend

Full support
Full support
No support
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

See also