(Map|Set|WeakMap)#set() returns `this` ?

Rick Waldron waldron.rick at gmail.com
Mon Dec 3 14:44:09 PST 2012


On Mon, Dec 3, 2012 at 5:21 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> IMHO, a set(key, value) should return the value as it is when you address
> a value
>
> var o = m.get(k) || m.set(k, v); // o === v
>
> // equivalent of
>
> var o = m[k] || (m[k] = v); // o === v
>
> a set with a key that returns `this` is a non case so almost as useless as
> the void return is.
>

> Usefulness comes with use cases ... except this jQuery chainability thingy
> that works fine for jQuery structure ( an ArrayLike Collection )
>

A collection is a collection.

who asked for map.set(k0, v0).set(k1, v1).set(k2, v2) ? Or even
> map.set(k0,v0).get(k1) ? what are use cases for this?
>
> I am honestly curious about them because I cannot think a single one ...
> specially with the Set
>
> s.add(k0).add(k1).add(k2) ... this code looks weird inlined like this ...
>

You're completely ignoring the iterator APIs and forEach—either of which a
program might want to call on an object post-mutation:

Add value to the Set and...

- get a fresh iterable for the values (or keys, or entries):

  set.add( value ).values();

- send each value in the set to another operation:

  set.add( value ).forEach( item => ...send to some operation.... );

- spread into an array of unique items:

[ ...set.add(value) ]; // always unique! yay!


Add a key and value to the Map and...

- get a fresh iterable for the keys (or values, or entries)

  map.set( key, val ).keys();
  map.set( key, val ).values();
  map.set( key, val ).entries();

- send each to pair to another operation (see above)

- spread into an array of pairs (see above)


Being able to express the complete operation and get mutated object back at
once is a compelling use case.

Rick


>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121203/83649911/attachment.html>


More information about the es-discuss mailing list