Strawman: Complete array and object destructuring

Paul Whipp paul.whipp at
Wed Mar 29 00:55:35 UTC 2017

The ... destructuring for arrays is an excellent improvement that makes
javascript code more readable and robust.

On seeing the syntax for the first time I expected to be able to use it
more completely. 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 form `[...objProps, prop]` fails as per the spec, as
would `[first,, last]` in spite of these being 'natural' uses that
I believe a naive programmer (me at least ;)) would expect to work.

As can be seen in the example above, if more complete destructuring were
supported, javascript code using it would be easier to read and write.

For objects, when the `...varName` is used on the LHS, I'd expect the
varName to be assigned to a new object containing only those properties not
extracted. In this case, as the order of keys in an object are arbitrary,
the position of this element in the LHS would not matter. In addition to
extracting properties from objects, this lets me have a shallow copy with
`const {...copy} = source;` or to 'pop' a property with something like `
{[propName]: propValue, ...obj} = obj}`.

The object destructuring could be considered a separate proposal if

-- Paul Whipp

PS: First contemplated here
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list