What is the status of Weak References?
nathan.wall at live.com
Fri Feb 1 21:41:04 PST 2013
David Bruant wrote:> >> David Bruant wrote:
> >> Garbage collectors have evolved and cycles aren't an issue any longer, weak
> >> references or not.> >> > Kevin Gadd wrote:
> > Cycles are absolutely an issue, specifically because JS applications
> > can interact with systems that are not wholly managed by the garbage
> > collector. The problem in this case is a cycle being broken *too
> > early* because the application author has to manually break cycles. To
> > present a couple simple examples:
> > I have a top-level application object that manages lower-level 'mode'
> > objects representing screens in the application. The screens, when
> > constructed, attach event listeners to the application object. Because
> > the application manages modes, it needs to have a list of all the
> > active modes.
> > * The event handler closures can accidentally (or intentionally)>> Last I heard, it's very difficult to accidentally capture a reference in
> a closure because modern engines check which objects are actually used
> (looking at variable names), so for an object to be captured in a
> closure, it has to be used. So "intentionally".
In this case, we managed to come up with a solution to refer to elements "weakly" through selectors, retrieving them out of the document only when they're attached (we have a single `setInterval` that always runs, but it allows objects to be GC'd). However, this solution is far from fool-proof, lacks integrity (any element can mimic our selectors and cause us grief), and not performant. In our case it's good enough, but I can imagine a case where it wouldn't be. I can also imagine a case where you wouldn't have the luxury to use DOM traversal as a "weak" mechanism for referring to objects. I think it could be useful internally in library components which make use of 3rd party components (in this case the DOM) to be able to monitor aspects of those components only when they're being consumed.
Having said that, I also understand the desire to keep the language deterministic and to not expose GC operations.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss