1

I've been using and loving babel (6.5.2) for a while now and find the new destructuring syntax great for writing clearer JavaScript.

Why doesn't the rest destructuring work (it generates a token error) anywhere in the array? For example:

          const [column, ...restOfColumns] = columns;
          const objProps = column.valueChain.slice(0, -1);
          const prop = column.valueChain[column.valueChain.length - 1];
          //const [...objProps, prop] = column.valueChain

The commented out line would replace the preceding two lines with something much easier to read and understand.

4
  • 1
    The word rest should be the hint: it can only be used for the rest of the array. Commented Mar 27, 2017 at 20:09
  • In general, when arbitrary-length data is used, it's at the end of something, so they didn't design a full pattern matcher. Commented Mar 27, 2017 at 20:10
  • The easy answer would be because the spec says so. Commented Mar 27, 2017 at 20:13
  • Thanks @Timo, I've raised this on [email protected] to see if I can get the spec to say different in future. Commented Mar 29, 2017 at 0:31

1 Answer 1

4

The simple answer is that when you use the destructing syntax ..., it means everything else. Therefore when you try [...objProps, prop], it doesn't know what to assign to prop as you have assigned all values already to objProps

Sign up to request clarification or add additional context in comments.

2 Comments

The ... syntax already works the way I suggest for objects. It is very handy being able to e.g. {x: 'x_default', ...original_object, ...object_revisions} for example. The ability to similarly construct and deconstruct lists is not just cool (although it is very cool), it is also logical and natural [first, ...rest, last] reads clearly and naturally for me.
This is a good answer, although I think the behaviour should be as posted in the question - changing it will mean a change to the specification in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.