Proposal: Named de-structuring arguments.

# Sultan (6 years ago)

The ability to both name and de-structure an argument.

Relying on the spread operator does not archive the same effect considering that the spread operator "by design" has some short-comings related to exotic objects when the objects in question have a rich prototype you want to preserve.

Consider the following proposal:

function fn (arg pick {a, b = arg.b = 'value'}) {}

This would allow four fundamental things of note missing from current de-structuring status-quo:

  1. The ability to have both named arguments and de-structuring.

  2. The ability to reference the named argument within the de-structure. This allows the above pattern of setting a default value on the passed argument when it doesn't exist.

  3. This in turn also affords you the ability to de-structure while also preserving the exotic nature of an object when it is not a POJO object.

That is when "arg" is an exotic use-defined object using the spread operator "{...arg}" will discard its prototype.

For example:

function a ({a, ...b}) { return b }

a([1, 2])

Returns a object with index-able keys instead of the array.

  1. Consequently this addition would also afford the avoidance of any overhead that one might want to avoid with the rest spread operation: {a, b, ...arg} in hot paths.
# Jordan Harband (6 years ago)
# Mark (6 years ago)

oof. I appreciate how terse it is, but the readability of it all is a bit scary when you're doing multiple assignments more than one level deep.