Destructuring into object
Bob Myers
rtm at gol.com
Mon Feb 26 05:55:28 UTC 2018
I presume you've read the historical threads on this topic, which go back
several years.
Bob
On Mon, Feb 26, 2018 at 11:04 AM, Alexander Shvets <quadratoff at gmail.com>
wrote:
> # The Problem
>
> ES6 destructuring syntax isn't very readable and useful, especially for
> assigning to properties of existing object:
>
> // ES6
> ({
> a: target.a,
> b: target.b,
> c: target.c,
> } = srcObject);
>
> // Versus ES3
> target.a = srcObject.a;
> target.b = srcObject.b;
> target.c = srcObject.c;
>
> There is no reasons to start using new ES6 destructuring syntax now, old
> syntax is better, because:
> - there is hard to assume that `target` object will be modified, if
> developer does not familar with new standart
> - identifiers are still duplicated
> - you should write braces `()` or use other solution, because JS statement
> cannot starts with `{`
>
> # Proposal
>
> So, in addition to current ES6 syntax, I want to propose this sugar:
>
> target.{a,b,c} = srcObject
> //alternative// target.* = srcObject{a,b,c}
>
> ## Why?
> + This syntax is more clear and understandable:
> even without ES6 knowleges, developer can assume that some properties
> assigned to the left-side `target` from a right-side `srcObject` variable.
> + The one of destructuring goal is met: full elimination of identifiers
> duplication.
> + You can omit braces `()` because JS statement doesn't starts with `{`
> now.
>
> #### New syntax for Object.assign()
>
> let target = {a:0}
>
> target.* = {b:1, c:2}
> //alternative// target.{} = ...
>
> console.log(target) // {a:0, b:1, c:2}
>
> Useful for:
>
> + adding properties to the `this` object in constructor
> + adding methods to the prototype of class
>
> #### Extracting properties into local variables
>
> let * = {a:1, b:2} // like import * from 'module'
> console.log(a, b) // 1 2
>
>
> GitHub Repo:
> https://github.com/transpiling/destructuring-into-object
>
> _______________________________________________
> 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/20180226/1721c449/attachment.html>
More information about the es-discuss
mailing list