简体   繁体   中英

JavaScript - How can I test the equality of WeakSet?

Is there a way to test if two (or more) WeakSets are equal? So, I want this code to print out true

var a = new WeakSet();
var b = new WeakSet();
a.add([3]);
b.add([3]);
console.log(isEqual(a, b)); // -> true

Simple answer - you can't. WeakSet s are not enumerable so there is no way to test the equality of values in a WeakSet .

Currently your only option is to use Array here.
Check out this answer .

Also see this proposal if you want to use Set instead.

Though I doubt that your use case requires that kind of memory management, you may also consider WeakReferences as part of your solution.

As a side note, WeakSet s have very few actual use-cases, so you probably should not settle for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM