Over lunch I was discussing ES6 and ES7 features with some developers doing 
JavaScript crypto, and they mentioned an interest in not only weak references, 
but weak callbacks. That is, a notification when your object gets garbage 
collected, similar (I think) to what V8 has with its `MakeWeak`, and what C# 
has with its finalizers.

Such a feature would be non-deterministic, since it depends on when the garbage 
collector runs; the callback may never run at all. But it could be useful for 
performing cleanup tasks related to a given weak reference. I imagine it could 
even be emulated with polling plus weak references, so there should be nothing 
fundamentally new here.

Personally I would find this feature somewhere between useful and an attractive 
nuisance. It would have to be carefully advertised as for last-ditch cleanup, 
not a substitute for deterministic disposal (via e.g. a `dispose()` method). C# 
has a similar dichotomy, with `IDisposable` for deterministic disposal, and a 
conventional pattern where you call `dispose()` in your finalizer if the 
consumer forgot to do so themselves.

What do people think of this feature?
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to