Set.prototype.entries: indices as keys?

Brendan Eich brendan at mozilla.org
Mon Jan 19 11:27:41 PST 2015


Brendan Eich wrote:
> Dmitry Soshnikov wrote:
>>
>>     ```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.
>
> See 
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-runtime-semantics-destructuringassignmentevaluation 
> -- destructuring array patterns uses the iteration protocol, which is 
> why the line works as Axel suggested:
>
> js> let set = new Set(['a', 'b']);
> js> let [x,y] = set;
> js> console.log(x, y)
> a b
> js> for (let elt of set) console.log(elt)
> a
> b
>
> But not all iterables are indexable, nor should they be. 

As you've pointed out (several times ;-), the odd duck is forEach. 
Set.prototype.forEach passes element as key and value. But sets cannot 
be indexed by values (to get booleans, as one might expect -- and even 
want, occasionally).

Sets should not be indexable but should be iterable. This leaves forEach 
with the dilemma of having a different signature on Set than Map and 
Array, or else taking a dummy or redundant parameter as the forEach key. 
Or we could leave forEach out of Set.prototype -- that's quite a better 
"different signature"!

/be


More information about the es-discuss mailing list