ECMAScript 6: what is WeakSet for?

The WeakSet is supposed to store elements by weak reference. That is, if an object is not referenced by anything else, it should be cleaned from the WeakSet.

I have written the following test:

var weakset = new WeakSet(),
    numbers = [1, 2, 3];

weakset.add({name: "Charlie"});


numbers = undefined;


Even though my [1, 2, 3] array is not referenced by anything, it's not being removed from the WeakSet. The console prints:

WeakSet {[1, 2, 3], Object {name: "Charlie"}}
WeakSet {[1, 2, 3], Object {name: "Charlie"}}

Why is that?

Plus, I have one more question. What is the point of adding objects to WeakSets directly, like this:

weakset.add({name: "Charlie"});

Are those Traceur's glitches or am I missing something?

And finally, what is the practical use of WeakSet if we cannot even iterate through it nor get the current size?



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.