Skip to content
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

Warn about accidentally destructuring to globals? #371

Open
tdd opened this issue Jan 25, 2017 · 3 comments
Open

Warn about accidentally destructuring to globals? #371

tdd opened this issue Jan 25, 2017 · 3 comments

Comments

@tdd
Copy link

tdd commented Jan 25, 2017

Hi Nicholas,

Location: Chapter 5 > Page 86 > "Destructuring Assignment" > first code block

Your examples all use destructuring in assigments (let or const), but it's also possible towards object properties, e.g.:

set fullName (value) {
  [this.first, this.last] = value.split(' ')
}

And, more dangerously, seems to retain JS' long-standing wart of "accidentally" declaring globals when using hitherto-unknown identifiers without a declaration keyword ahead of them (agreed, strict mode would kill this, but we're not always in strict mode, unfortunately).

Do you think this would be a good warning to add somewhere in there?

@tdd
Copy link
Author

tdd commented Jan 25, 2017

By the way, you do mention it can destructure to object properties and existing vars, in the penultimate par on page 97, but it would be great to mention that in the original explanation, too.

@nzakas
Copy link
Owner

nzakas commented Jan 25, 2017

Sorry, I'm not quite sure what you're asking here. Can you be more specific?

@tdd
Copy link
Author

tdd commented Jan 25, 2017

Sorry I was unclear. Let me try again.

Destructuring works in declarations (var, let, const), function signatures, and generally on the left side of assignments, which besides decls would include assigning to object properties and existing vars.

Your example code for this chapter focuses on declaration scenarios.

  • On the one hand, I believe it would be a nice complement if the code and/or surrounding text also made other acceptable scenarios (e.g. obj props, existing vars) more obvious.
  • On the other hand, declaration scenarios present the usual JS risk of people not understanding what happens when the declaring keyword (e.g. let) is omitted. In strict mode, to be fair, this would throw. But loose mode will, as usual, create globals unbeknownst to the programmer. For instance:
function poorlyPickTwoProps (obj) {
  { foo, bar } = obj
  return { foo, bar }
}

(yes, this would be much more nicely done by destructuring in the sig, bear with me 😉)

Here, we unwittingly introduce two globals: foo and bar. This is a long-standing JS wart, and indeed people coming to JS tend to forget var or let sometimes, so perhaps warning them against this side-effect would be useful.

Is that clearer, or am I botching this again? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@tdd @nzakas and others