Destructuring into object

# Alexander Shvets (6 years ago)

An HTML attachment was scrubbed... URL: esdiscuss/attachments/20180226/28570d9a/attachment

# Bob Myers (6 years ago)

I presume you've read the historical threads on this topic, which go back several years. Bob

# Alex Shvets (6 years ago)

Yes, I've read it, but I doesn't find something like this:

target.{a,b,c} = srcObject

Thank You!

# Bob Myers (6 years ago)

You make an interesting point. The current proposal focuses on picking, meaning to make an entirely new object by plucking out properties. It doesn't envision setting properties into an existing object.

Rather than thinking about picking as an extension to object literal syntax, as I had been proposing in the following:

{ {p1, p2} = o }

which some people found a bit challenging, and to accomplish what you want, would require saying

target = { ...target, {a, b, c} = srcObject};

We can think of your syntax

target.{a,b,c} = srcObject;

as a kind of natural, obvious extension of dot notation. In other words, whereas target.a = 42 sets the value of the a property of object target to 42, your syntax sets multiple properties. It's almost as if JavaScript had been waiting for two decades for this natural extension to dot-based assignments to object properties.

The corresponding accessor syntax--reading out multiple property values--then obviously becomes

target.{a, b, c}

which is an equally natural, obvious extension of dot notation. In other words, whereas target.a obtains the value of the a property of object target, this syntax obtains the value of multiple properties.

Given this nice symmetry and parity with other features, I have revised my proposal back to an earlier version, where the picking syntax was obj.{prop1, prop2}. The revised version of the proposal, including putting this structure on the left side of an assignment, is at rtm/js-pick-notation.

To me, things seem pretty simple:

  1. There is a strong IRL need need for "picking"--plucking out, or setting multiple properties at a time.
  2. Existing dot notation such as a.b is almost begging us to extend it to allow this in the form of a.{b,c}.
  3. The proposed extensions to dot notation have very small syntactic footprint.
  4. In doing this, we can take advantage of the powerful deconstructing assignment syntax already introduced.

Bob