JavaScripture
Contribute via GitHub Feedback

FinalizationRegistry : Object

FinalizationRegistry provides a way to be notified when an object is garbage collected. Note, JavaScript makes no guarantees on if or when an object will be garbage collected. See also WeakRef.

Constructors

new FinalizationRegistry(cleanupCallback : Function) : FinalizationRegistry
cleanupCallback(heldValue : Object) : undefined

Returns a new FinalizationRegistry that will call cleanupCallback when objects passed to register() are garbage collected.

Example:

Run

Results:

 

Instance Methods

register(target : Object, heldValue : Object, [unregisterToken : Object]) : undefined

Registers target with this so the cleanupCallback passed to this' constructor will be called when target is garbage collected.

The value passed to cleanupCallback at that time will be heldValue. Note target and heldValue can not be the same object because this will hold a strong reference to heldValue, preventing it from being garbage collected.

If unregisterToken is specified, you can prevent cleanupCallback from being called by calling unregister(unregisterToken).

Example:

Run

Results:

 

unregister(unregisterToken : Object) : undefined

Prevents cleanupCallback from being called for an object previously registered with the specified unregisterToken.

Example:

Run

Results: