JavaScripture
Contribute via GitHub Feedback

WeakRef : Object

A WeakRef allows holding on to another object, while still allowing that object to be garbage collected if there are no other strong references to it. Note, JavaScript makes no guarantees on if or when an object will be garbage collected. See also FinalizationRegistry.

Constructors

new WeakRef(target : Object) : WeakRef

Creates a new WeakRef that tracks target.

Instance Methods

deref() : Object

Returns the target of this if it has not been garbage collected yet, otherwise returns undefined. If deref() returns a valid object, that object will not be garbage collected until control returns to the event loop, such as when returning from a callback or awaiting a Promise.

Example:

Run

Results: