for statement with index and value
Tingan Ho
tingan87 at gmail.com
Tue Jul 14 04:32:41 UTC 2015
Yes the proposed syntax is a special case for arrays.
tis 14 juli 2015 kl 12:23 skrev Edwin Reynoso <eorroe at gmail.com>:
> Something wrong with server that doesn't let me edit.
>
> But what I meant by the first code snippet was:
>
> ```JS
> for(let a, b of new Set([1,2])) // what would `a` and `b` be here? How
> would it know what to extract??
> ```
> Would `b` just be `undefined`, yet for an array it returns the `index` how
> does it determine that unless again this is special to Arrays?? because
> `b/index` could be anything else, that's not obvious compare to
> destructuring.
>
>
> On Tue, Jul 14, 2015 at 12:13 AM, Edwin Reynoso <eorroe at gmail.com> wrote:
>
>> So I'm assuming this would be special to arrays??
>>
>> because destructuring works fine for anything that's iterable:
>>
>> meaning how would it know what to take out for Sets??
>>
>> ```JS
>> for(let value, index of [1,2]) {
>> // do something
>> }
>> ```
>>
>> With destructuring we at least know what's being extracted (not sure if
>> destructured would be the right word, clueless on that):
>>
>> ```JS
>> let it = [1,2].entries();
>> let [index, value] = it.next();
>> // same as:
>> let [index, value] = [0, 1];
>> // the matching is obvious
>> ```
>>
>> With your suggestion it's not obvious:
>>
>> ```JS
>> for(let value, index of [1,2]) // how does it know what value and index
>> would be??
>> ```
>>
>> I don't think this would be done if it's only for Arrays.
>>
>> On Tue, Jul 14, 2015 at 12:04 AM, Tingan Ho <tingan87 at gmail.com> wrote:
>>
>>> >Unfortunately we can't have both...
>>> ```
>>> for (let [index, value] of values){
>>> ```
>>>
>>> I was suggesting the syntax:
>>> ```
>>> for (let value, index of values){
>>> ```
>>> `value` comes first and no `[ ... ]`.
>>>
>>>
>>>
>>> On Tue, Jul 14, 2015 at 11:52 AM, Logan Smyth <loganfsmyth at gmail.com>
>>> wrote:
>>>
>>>> Unfortunately we can't have both
>>>>
>>>> ```
>>>> for (let value of values){
>>>> ```
>>>>
>>>> and
>>>>
>>>> ```
>>>> for (let [index, value] of values){
>>>> ```
>>>>
>>>> Over all, the first one is the more likely one on a day-to-day basis.
>>>>
>>>> The `[]` are needed because the `for...of` follows the standard rules
>>>> for assignment, so it uses standard destructuring, and JS array
>>>> destructuring requires `[]`.
>>>>
>>>> ```
>>>> for (let [index, value] of values.entries()){
>>>> ```
>>>>
>>>> is essentially is the same as
>>>>
>>>> ```
>>>> for (let pair of values.entries()){
>>>> let [index, value] = pair;
>>>> ```
>>>>
>>>> As for your last question, `.entries` returns an iterator, so it will
>>>> not create a copy of the array.
>>>>
>>>> On Mon, Jul 13, 2015 at 7:43 PM, Tingan Ho <tingan87 at gmail.com> wrote:
>>>>
>>>>> >for (let [index, value] of [1, 2, 3].entries())
>>>>> console.log(index + ": " + value)
>>>>>
>>>>> I still think most people will write:
>>>>>
>>>>> ```
>>>>> for (let value of values) { ... }
>>>>> ```
>>>>> and then rewrite the whole expression inside the `for-loop` when they
>>>>> find out that they need the index too:
>>>>> ```
>>>>> for (let [index, value] of [1, 2, 3].entries())
>>>>> console.log(index + ": " + value)
>>>>> ```
>>>>> `for (let value, index of values) { ... }` is still much easier to
>>>>> type than `for (let [index, value] of [1, 2, 3].entries())` and also more
>>>>> readable.
>>>>>
>>>>>
>>>>> Also, doesn't that makes a copy of the `[1, 2, 3]`?
>>>>>
>>>>> --
>>>>> Sincerely,
>>>>>
>>>>> Tingan Ho
>>>>> @tingan87 <https://twitter.com/tingan87>
>>>>>
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss at mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>> Sincerely,
>>>
>>> Tingan Ho
>>> @tingan87 <https://twitter.com/tingan87>
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150714/1cb8be47/attachment-0001.html>
More information about the es-discuss
mailing list