2

I don't get what usage those two classes could have that Set/Map don't cover.

If there something inside a weakset/weakmap, you can't access it/ask about it withouth a reference to the objects/keys themselves.

Since you need a reference to the object/key to query weakset/weakmap, there's no way the weakreference was released to begin with.

(Me) "Hey, WeakSet, here is some object you knew about, do you still does?"

(WeakSet) "have you ever asked me to forget about it?"

(Me) "No."

(WeakSet) "And do you have a reference to this object?"

(Me) "Of course, how else would we be talking about it?"

(WeakSet) "Exactly! Now stop asking stupid question."

If one just need to make sure an element has already been put in a set or used as a key of a map, one'd rather just use Set and Map (imo).

Could it be those are currenlty of no use?

Serge Intern
  • 2,669
  • 3
  • 22
  • 39
  • *`you can't access it/ask about it* - what if you just iterate? – Bartek Banachewicz May 05 '17 at 07:15
  • You can't iterate over them – Serge Intern May 05 '17 at 07:16
  • You're describing the case where there *are* still other references to a key object. If there are *not* still other references to a key object, *then* it won't be kept alive unnecessarily like it would be if in some other structure. (Of course, if you *want* it to be kept alive in such a case don't use `WeakMap`.) See also [this question](http://stackoverflow.com/questions/29413222/what-are-the-actual-uses-of-es6-weakmap). – nnnnnn May 05 '17 at 07:21
  • Have a look at this http://stackoverflow.com/questions/15604168/whats-the-difference-between-es6-map-and-weakmap – Thabo May 05 '17 at 07:24
  • @nnnnnn you're describing the behaviour. It doesn't say what it is good for (that a Map couldn't do as easily)? – Serge Intern May 05 '17 at 07:25
  • 2
    Just speculating here, but I think the main use is for referencing objects, to which you do not keep a separate reference, but rather are added through other collections, most notably the DOM. If you say that the weakset is useless if you have to keep a variable to actually check something in the weakset, I'd have to agree it's of no use, but when referencing objects from another collection, you want to be able to check later if they've been handled (or such) without keeping a hard reference, weakset/maps can save the day – Me.Name May 05 '17 at 07:29
  • @Me.Name thanks, it answers the question. I can see the point. – Serge Intern May 05 '17 at 07:36
  • 1
    Imagine you want to store additional information about an object that is managed by other code. You can't add more properties to the object itself without messing up the other code, so one way would be to use it as a `WeakMap` key with the extra info as the value. Next time the other code tells you to do something with the object you still have your extra info in the weak map. But if the other code stops referencing it you don't retain an unneeded reference yourself. (Edit: Or maybe I've just repeated @Me.Name's point using different words.) – nnnnnn May 05 '17 at 07:37

0 Answers0