javascript - ECMAScript 6: what is WeakSet for? -
the weakset supposed store elements weak reference. is, if object not referenced else, should cleaned weakset.
i have written following test:
var weakset = new weakset(), numbers = [1, 2, 3]; weakset.add(numbers); weakset.add({name: "charlie"}); console.log(weakset); numbers = undefined; console.log(weakset);
even though [1, 2, 3]
array not referenced anything, it's not being removed weakset. console prints:
weakset {[1, 2, 3], object {name: "charlie"}} weakset {[1, 2, 3], object {name: "charlie"}}
why that?
plus, have 1 more question. point of adding objects weaksets directly, this:
weakset.add({name: "charlie"});
are traceur's glitches or missing something?
and finally, practical use of weakset if cannot iterate through nor current size?
it's not being removed weakset. why that?
most because garbage collector has not yet run. however, using traceur, might they're not supported. wonder how console
can show contents of weakset
anyway.
what point of adding objects weaksets directly?
there absolutely no point of adding object literals weakset
s.
what practical use of weakset if cannot iterate through nor current size?
all can 1 bit of information: object (or generically, value) contained in set?
this can useful in situations want "tag" objects without mutating them (setting property on them). lots of algorithms contain sort of "if x
seen" condition (a json.stringify
cycle detection might example), , when work user-provided values use of set
/weakset
advisable. advantage of weakset
here contents can garbage-collected while algorithm still running, helps reduce memory consumption (or prevents leaks) when dealing lots of data lazily (possibly asynchronously) produced.
Comments
Post a Comment