WeakRef.prototype.deref()

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

Syntax

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
ECMAScript Language Specification
# sec-weak-ref.prototype.deref

Browser compatibility

BCD tables only load in the browser

See also