Set.prototype.entries: indices as keys?

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Sun Jan 18 22:01:39 PST 2015


On Sun, Jan 18, 2015 at 4:28 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

> Currently the keys of the entries returned by `Set.prototype.entries()`
> are the same as the values:
>
> ```js
> let set = new Set(['a', 'b']);
>
> let pairs = [...set.entries()];
> console.log(JSON.stringify(pairs)); // [["a","a"],["b","b”]]
> ```
>

To begin with: `.entries()` doesn't make much sense in Sets abstraction at
all (as well as `.keys()`). But the ship was sailed.

Sets are just a sugar on top of maps in ES6 (with just renamed API methods).


>
> Given that sets are ordered, I’d use the “position” of an entry as the
> key: [[0,"a"],[1,"b”]]
>

This notation would assume that a set may have non-unique values (e.g. "a"
at index 0, and "a" at index 2), and in this case it wouldn't be a set
anymore.


>
> Rationale: First, having an indices as keys makes the entries more useful.
> Second, destructuring already treats entries as if they had indices:
>
> ```js
> let [x,y] = set; // x='a'; y='b’;
> ```
>

Would this work actually? :) Destructuring does get property, which
wouldn't call set's `get` method.

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


More information about the es-discuss mailing list