0

I need to store weak referenced object with its id (integer) and get the object later by id. And I will expect getting undefined or null from the id once it's GCed.

ES2015 introduces WeakMap and WeakSet. However they both don't allow primitive types as key and don't allow iteration.

Is it possible to achieve this?

NOTE: I'm aware that WeakMap and WeakSet are weak in keys. I'm asking whether it is possible to implement function mentioned above, via WeakSet or WeakMap or other ways in JS.

Ghostoy
  • 2,689
  • 19
  • 18

1 Answers1

0

It is not possible to hold an arbitrary reference to an object weakly. There is a proposal to add such an API, but it is not currently possible. https://github.com/tc39/proposal-weakrefs

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251