4

Does JavaScript destructuring have syntax to capture both an object and its content?

In other words, can I do the following completely in the function's arg list without the following const line?

f = (a) => {
  const {b} = a;
  console.log("I see:", a, "and", b);
}

f({b:42})

==> I see {b: 42} and 42

(FWIW: I'm thinking of something like :as in Clojure or ClojureScript).

1
  • Not in a parameter list, no - your solution in the body is fine. In declarations you can do const outer = a, {b} = a; and in nested destructuring you can do const {outer, outer: {b}} = {outer: a}; Commented Feb 28, 2019 at 17:47

2 Answers 2

5

Yes you can

const f = (a, {b} = a ) => {
  console.log("I see:", a, "and", b);
}

f({b:42})

For more use cases of destructuring assignment you can see this

On side note:- Always keep one extra parameter than you in function definition than than your function call.

Update With this way you need not to worry about one extra parameter in function definition

const f = (...z) => (a=z[0],{b}=z[0],...arguments) => {
  console.log("I see:", a, "and", b, z);
}

f({b:42},{b:32})()

You can try it with IIFE's too @bergi thanks for inputs.

const f = (a, ...z) => (({b}) => {
  console.log("I see:", a, "and", b, z);
})(a);

f({b:42},{b:32})

Sign up to request clarification or add additional context in comments.

8 Comments

cool trick but if you (by mistake) pass something as a second argument to the function everything breaks. It's safer to use destructuring in the function body.
@marzelin when you intend to do something like this, means you're always aware of what you're doing ? isn't it ?
@CodeManiac from my point of view doing something like this is just asking for trouble. People make mistakes unfortunately.
-1, this is not a comma operator. It's a delimiter between multiple parameters, and should absolutely not be used here.
@CodeManiac If you go for IIFEs as a fill-in for let expressions, I do it like this: jsfiddle.net/meusvptn :-)
|
0

This is a way

const f = (a, {b}=a) => {
  console.log("I see:", a, "and", b);
}

f({b:42})

The problem comes up when you need to pass more than one parameter.

const f = (a, {b}=a) => {
  console.log("I see:", a, "and", b);
}

f({b:42},123)

1 Comment

-1, this is not a comma operator. It's a delimiter between multiple parameters, and should absolutely not be used here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.