I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener.
So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time.
Is this possible to use WeakMaps in some clever way I haven't thought of to do this?
I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener.
So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time.
Is this possible to use WeakMaps in some clever way I haven't thought of to do this?
javascript
weak-references
ecmascript-6
ecmascript-harmony
weakmap
Share
Improve this question
edited Apr 19, 2015 at 21:45
Bergi
666k161161 gold badges1k1k silver badges1.5k1.5k bronze badges
asked Apr 3, 2015 at 16:31
Michael LortonMichael Lorton44.4k2626 gold badges107107 silver badges156156 bronze badges11
1Just curious: What kind of architecture do you envision? Who is holding the references to the event listeners, is keeping them alive? This wouldn't work in the current designs where event listeners execute side effects.
– Bergi
CommentedApr 19, 2015 at 21:42
1What actual problem are you trying to solve? As WeakMaps don't work (see my answer), I might be able to suggest some alternative solutions.
– Bergi
CommentedApr 19, 2015 at 21:44
2@dandavis -- functions can use up lots of resources if there are any closure references contained within.
– Michael Hays
CommentedApr 25, 2015 at 0:41
1Operate on its parameters. Return a result. Control external resources. The same sorts of things functions do in languages that don't have closures, I'd imagine. ;-) @dandavis has a good point -- if your handlers do not have closures (perhaps they look things up on a table before taking action), then the overhead of keeping around a list of functions is not expensive. This is, of course, what you should do in a language that doesn't support weak references.
– Michael Hays
CommentedApr 25, 2015 at 4:36
1I'm not here to argue. My ment was to dandavis -- specifically that functions can and do use up a lot of resources. You seem to be agreeing with me. So I'm not sure what the point is that you are trying to make to me. Maybe you meant to address your original ment to dandavis?
– Michael Hays
CommentedApr 25, 2015 at 17:27
|
Show 6 more ments
1 Answer
1
Reset to default
16
+50
No, it is impossible to use WeakMaps to create a weak reference. WeakMaps are not iterable, to use them you always need the key. This was a deliberate decision (also here), so that garbage collection does not influence the semantics of your program - which is exactly what you want.
Real weak references might e with ES8, see here and there for drafts.