getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.28k stars 33.42k forks source link

ES6 & Beyond - Chapter 2: Object destructuring: preferred syntax #1676

Closed floratmin closed 4 years ago

floratmin commented 4 years ago

Please type "I already searched for this issue": I already searched for this issue Edition: (1st or 2nd) 1st Book Title: ES6 & Beyond Chapter: 2 Section Title: Destructuring Question: You wrote on page 26: "I would have preferred the syntax to be { AA: x, BB: y } for the destructuring assignment, ..." On page 29 you gave an example of destructuring objects which answers the question why the destructuring was realized in this way: var { a: { b: [ c, d ], e: { f }, g } = obj; This 'natural' destructuring would not be possible with the switched syntax.

getify commented 4 years ago

would not be possible with the switched syntax.

I believe it would have worked...

var { { [ c, d ]: b, { f }: e, g } = obj;

It makes ":" mean "from" instead of "to", just like "=" means "from".


Moreover, the book is not contradicting itself. I can simultaneously assert what I would have wanted (initially when first encountering something) and also assert what I later understand to be a reasonable way to accept the different way.