-
Notifications
You must be signed in to change notification settings - Fork 22.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Destructuring assignment": Need better introduction to object-destructuring syntax #38224
Comments
The Syntax section is an unfortunate situation. It is impossible to make it better, without deviating from our dogma that "Syntax has to present all possible valid combinations of source constructs, but without using BNF". I'm not convinced that they are bad—if you don't understand them you can ignore them and read on. There are examples with every description anyway.
You've already read past the intro. The code you are quoting demonstrates one specific thing: the difference between a binding pattern and an assignment pattern. What values the variables have is irrelevant; only that new variables are created is. The very first section under "Description" already tells you what values they have: const obj = { a: 1, b: 2 };
const { a, b } = obj;
// is equivalent to:
// const a = obj.a;
// const b = obj.b; You should be able to that they are 1 and 2 respectively, and gives you the underlying mental model for how to desugar them. Renaming is a much much rarer usage than destructuring in general. And it is my opinion (and many others') that you should not destruct if you need to rename. Furthermore, the description is general: it purposedly does not talk about anything specific to object or array patterns. Anything that only object destructuring can do (such as renaming) or only array destructuring can do (such as invoking the iterator protocol) is deferred to the "examples" section. I think they are given the right amount of attention that they need. Nevertheless I agree that the docs are still a bit dense, sometimes underexplaining sometimes too verbose. If you have edits you want to make, I'm happy to review a PR, but I'm not sure there's anything specifically actionable for me or for others. |
Thanks for responding.
*nod* I can accept that, and I do see the value of maintaining consistency unless there's a strong imperative to deviate from it, which I don't think is the case here.
I mean, yeah, but the intro consists of, in toto:
(Rearranging your statements a bit...)
Oh, agreed, for that code. There's no real need for inference, because the last two lines explicitly give the values of the variables created. (The reader can be expected to understand that
See, this is where you lose me, because:
I agree that the values of the variables are irrelevant, but I don't think what they're created to represent is irrelevant. Nor do I think it's especially obvious. The code I quoted could be expanded like so, if we follow the style of the earlier example you quoted: const obj = { a: 1, b: { c: 2 } };
const {
a,
b: { c: d },
} = obj;
// is equivalent to:
// const a = obj.a;
// const d = obj.b.c; So there we've demonstrated another binding destructuring form, but introduced the added complexities of both inner destructuring, and renaming — without comment on either. Even explicitly spelled out like that, I can imagine a first-time reader seeing (In fact, explicitly spelling it out like that makes that reaction slightly more likely. But only because it's likely to sail right over their heads completely UNLESS it's spelled out.) |
MDN URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
What specific section or headline is this issue about?
No response
What information was incorrect, unhelpful, or incomplete?
I apologize for the fact that this is a somewhat "squishy" report. IMHO, the destructuring documentation in its current form still fails to adequately present the intricacies of how object destructuring (specifically) works. Most of the information is there, but (again, IMHO) too much of it is introduced by example long before it is explained, which can be confusing to readers. If you already understand how destructuring works, the documentation is fine, but as an introduction to it for a developer who doesn't already know, it seems sub-optimally organized.
The main issues, as I see it, are...
The Syntax section is an overwhelming wall of possible forms
Especially since every form is effectively doubled. For instance, i'm not convinced it's helpful to present both of these in their entirety:
A similar redundant pair is presented for object-destructuring syntax.
The object-destructuring examples makes logical leaps that may not be as obvious as they're assumed to be
For instance, in this example:
...it's never even explicitly mentioned that
a
will have the value1
andd
will have the value2
, much less how property names and values are mapped to variable names in object-destructuring syntax.Assigning to new variable names is only explained much, MUCH farther down the page
Eventually, the docs do cover that...
But even that seems out of left field, because it's NEVER ONCE mentioned that object-destructuring by default creates variables with the same name as the object property! (And the early example involving
a
andd
(above) already demonstrated the "assigning to a different variable name" case, just without providing any explanation for what was going on.)What did you expect to see?
I think it would benefit readers, particularly less experienced ones, to present more detailed explanations, earlier in the documentation, of the process by which object destructuring maps variable names. In other words, explanations that take the reader through these equivalencies:
Do you have any supporting links, references, or citations?
No response
Do you have anything more you want to share?
No response
MDN metadata
Page report details
en-us/web/javascript/reference/operators/destructuring_assignment
The text was updated successfully, but these errors were encountered: