3

I have a decent handle on Rest, Spread, and Destructuring in Javascript.

Even so, I have been unable to figure out what exactly is happening in the following code:

const { ...foo } = [1, 2];
console.log(foo);

I have broken down the steps in my head, as follows:

  1. ...foo creates a new array, a copy of [1, 2]
  2. The object destructuring should, to my understanding, unpack an object on the right-hand-side, and place it into new variable(s). This is where I fall off. Above, the code behaves such that an array from the right-hand-side is seen as an object, and a new object is created with each property equal to each element of the array. That does not happen in a destructuring assignment without rest: const { foo } = myObj; <-- No new object is created here, foo becomes a constant equal to the foo property in myObj.

Questions


  1. What exactly is happening in the destructuring assignment?
  2. Could someone demonstrate the steps that occur above, without using destructuring?
  3. Is there a place in the ECMA spec (or similar) where the behavior is explained?
7
  • 2
    try console.log({...[3,4]}) . since arrrays are also object in js such that you can say that first key is 0 and so on. this is how i understood. also you can access arrays as if you would access an object. for example [3,4][0] would give 3 and {0:3,1:4}[0] would also give 3 Commented Nov 26, 2022 at 15:17
  • It is almost as if no destructuring really happens. That, in fact, what is going on is simply that we create an object literal, and then iterate through the array to create properties for each array element. Bizarre.. Commented Nov 26, 2022 at 15:24
  • Thanks @cmgchess, but that does not explain what is actually happening here, step by step. The destructuring assignment somehow creates a new object, where it has converted each array element into a property. I am trying to understand what happens under the hood. Commented Nov 26, 2022 at 15:31
  • The bizarre part is when you destructure an array into an object like that you would expect that foz has a length property but it doesn't. I guess that array has some special treatment in this scenario. Commented Nov 26, 2022 at 15:31
  • Since the first assignment is clear to you, I believe it should not be part of the question. Commented Nov 26, 2022 at 15:36

1 Answer 1

3

What exactly is happening in the destructuring assignment?

A destructuring assignment where the left side has [ ...rest] notation will create an array for rest. When it has { ...rest} notation, it will create a plain object for rest.

In the first case, the array values from the right hand side are iterated, and these populate the left hand array.

In the second case, the object entries from the right hand side are iterated, and these define the key/value pairs of the left hand object.

Could someone demonstrate the steps that occur above, without using destructuring?

Like this:

let foo = {};
for (const pair of [1, 2].entries()) {
    foo[pair[0]] = pair[1];
}

console.log(foo);

Is there a place in the ECMA spec (or similar) where the behavior is explained?

The ECMA Script reference is at Destructuring Assignment, and the ObjectAssignmentPattern with AssignmentRestProperty. In that section you'll find Runtime Semantics: RestDestructuringAssignmentEvaluation.

There you see a plain object is created (in step 2). In step three the procedure CopyDataProperties ( target, source, excludedItems ) is executed. This copies enumerable (see step 4.c.ii) properties of the source object (in your case an array object) into the target object.

It is important to stress that only own, enumerable properties are included, which explains why for instance the length property is not copied into the target object.

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

2 Comments

Thanks this also answers my question. It is funny that length property is an example for Object.prototype.propertyIsEnumerable() method.
Awesome, the ECMA pointer completed the picture for me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.