W3cubDocs

/JavaScript

weakRef.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

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
deref 84 84 79 No No No
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
deref 84 84 79 No No No
Server
Node.js
deref 13.0.0
Disabled
13.0.0
Disabled
Disabled From version 13.0.0: this feature is behind the --harmony-weak-refs runtime flag.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref