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

What about destructuring? #2

Open
targos opened this issue Nov 26, 2017 · 4 comments
Open

What about destructuring? #2

targos opened this issue Nov 26, 2017 · 4 comments
Labels

Comments

@targos
Copy link

targos commented Nov 26, 2017

For example:

function f({ foo, bar: {xyz} }) {
  console.log(foo, xyz);
}

I suppose it should be like this:

function f(const { foo, bar: {xyz} }) {
  console.log(foo, xyz); // both are const
}
@ljharb
Copy link

ljharb commented Nov 26, 2017

Good question.

One alternative is "like destructuring", where you can't do multiple of const/let/var in the same destructuring. However, the workaround there is simply to do one destructuring statement per variable type.

Here, you'd probably want to be able to specify each binding separately. Perhaps:

function f({ const foo, notConst, bar: { const xyz } }) {
}

But I suspect that going that route would require changing object destructuring itself, for consistency. It might still be worth it.

@Fishrock123
Copy link
Owner

function f({ const foo, notConst, bar: { const xyz } }) {
} 

Is that actually desirable?

I feel like it would work as similar to const variable declarations as possible, i.e.

function f(const { your, destructured, properties }) {
}

@ljharb
Copy link

ljharb commented Nov 26, 2017

@Fishrock123 I think that it should work like regular destructuring, which indeed would require that example: ie, const { <destructuring pattern> }. However, this limitation in object destructuring isn't a big deal when you can have multiple statements, or (like in function arguments) when the only option is var. If we're going to add const as an available option in function arguments, and since there's only one opportunity to destructure a function argument in the signature, I think the need for inline const/let/var disambiguators in destructuring manifests.

@not-an-aardvark
Copy link

If we're going to add const as an available option in function arguments, and since there's only one opportunity to destructure a function argument in the signature, I think the need for inline const/let/var disambiguators in destructuring manifests.

It seems like there would be a simple workaround for this -- the user could just not use destructuring if they want only some of the destructured parameters to be const.

function f({ const foo, notConst, bar: { const xyz } }) {
}

// could be replaced with

function f(const arg) {
  const { foo, bar: { xyz } } = arg;
  var { notConst } = arg;
}

This would be a bit tedious, but I suspect this isn't a very common use case. (Personally, I would make all of my parameters const, and create a local alias with let if I really needed a modifiable reference.) It seems like it would be better to be consistent with the existing variable-destructuring syntax rather than complicating the syntax just for this use case.

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

No branches or pull requests

4 participants