1

In looking at JavaScript from various Firefox extensions, I've seen code like the following for creating aliases (example):

let Cc = Components.classes;
let Ci = Components.interfaces;
let Cu = Components.utils;

This makes sense to me. However, I've seen a strange looking variant of this code that looks like this (from this example):

let { classes: Cc, interfaces: Ci, utils: Cu } = Components;

I don't fully understand this assignment statement. I believe that the end result is essentially the same as the former code block, but why? An anonymous object is being assigned the value Components, correct? I've never seen an assignment type like this before, so it makes little sense to me.

I looked at the Values, variables, and literals page at MDN, and the documentation for let, but neither page had any examples of this type of construction.

Jonah Bishop
  • 11,967
  • 5
  • 44
  • 72

1 Answers1

1

The second form is a new feature of ECMAScript6, called destructuring assignment. You won't see it in the open web due to lack of support, but you can find it in some code running on node.js or in firefox/addon internals. Or things that are run through es6 to es5 transpilers.

See MDN docs for further details.

the8472
  • 37,986
  • 4
  • 66
  • 115