WeakSet can be very useful in general to avoid *any* object to be
visited/setup twice, not just those coming from user-land classes.

Circular references, mixins, DOM nodes one-off events handling, and so on
and so fort.

On Mon, Apr 22, 2019 at 8:26 PM #!/JoePea <[email protected]> wrote:

> (I edited the broken format of my previous post)
>
> What other use cases are there?
>
> On Mon, Apr 22, 2019 at 11:20 AM #!/JoePea <[email protected]> wrote:
>
>> > WeakSets are perfect for branding and are how I would expect web
>> platform class branding to be explained.
>> >
>> > ```js
>> > const foos = new WeakSet();
>> >
>> > class Foo {
>> >   constructor() {
>> >     foos.add(this);
>> >   }
>> >
>> >   method() {
>> >     if (!foos.has(this)) {
>> >       throw new TypeError("Foo.prototype.method called on an
>> incompatible object!");
>> >     }
>> >   }
>> > }
>> > ```
>>
>> Just curious, is that effectively the same as what the (current) [private
>> fields proposal](https://github.com/tc39/proposal-class-fields) offers?
>>
>> ```js
>> class Foo {
>>   #isFoo = true
>>
>>   method() {
>>     if (this.#isFoo) {
>>       throw new TypeError("Foo.prototype.method called on an incompatible
>> object!");
>>     }
>>   }
>> }
>> ```
>>
>> - Joe
>>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to