34

I'm doing some FF add-on development and I'm seeing syntax like this:

var {Cc, Ci} = require('chrome');

Just curious what that syntax is and if it's special to FF development or something else.

Bialecki
  • 30,061
  • 36
  • 87
  • 109
  • How can I prevent it to be appear like error in Visual Studio + Resharper? Since it is not recognized as natural syntax of javascript, ReSharper keeps showing it as an syntax error and cannot reformat document. – Teoman shipahi Aug 26 '14 at 15:02
  • How did this *earlier* question get marked as a dupe of the same thing asked in 2013? – ruffin Apr 22 '17 at 18:04

2 Answers2

34

This is called destructuring assignment. It is a feature of JavaScript 1.7, where in this context "JavaScript" refers to Mozilla's specific extensions to the ECMAScript standard.

It is slated for inclusion in the next version of JavaScript.

The equivalent ECMAScript 5 code would be

var __temp = require('chrome');
var Cc = __temp.Cc;
var Ci = __temp.Ci;
Domenic
  • 110,262
  • 41
  • 219
  • 271
  • Thanks a ton, i finally got rid of the syntax error in my editor and my code is being evaluated again. – razz Aug 04 '14 at 23:19
  • But does it actually cause a variable `__temp` to come into existence? If not then the equivalence is only a rough one. – hippietrail May 23 '16 at 15:40
9

See Domenic's answer as to what the syntax is which is called a destructuring assignment. The answer that follows is why this is needed for FF add-on development.

There's a discussion on what this is and why it is needed at http://groups.google.com/group/mozilla-labs-jetpack/browse_thread/thread/d288b79903b5b434.

Short answer is yes, it's specific for Firefox add-on development. The relevant documentation can be found at https://addons.mozilla.org/en-US/developers/docs/sdk/1.3/dev-guide/module-development/chrome.html.

Bill
  • 25,119
  • 8
  • 94
  • 125
  • 7
    Wrong answer. It’s ECMA 6 syntax, and just because Firefox is the first browser implementing destructuring assignments doesn’t mean it’s only relevant for FIrefox addon development. – flying sheep Oct 23 '14 at 14:11
  • 2
    I think I answered the wrong question - I thought they were asking why you now need to `require('chrome')` for FF add-ons. Unfortunately the answer was accepted and so I cannot delete it. – Bill Oct 23 '14 at 16:17
  • Ah, got it! I thought it was pretty obvious that the `{...} =` syntax is the weird thing here, as `require('...')` is a simple function call. But the question isn’t unambiguous, you’re right! – flying sheep Oct 24 '14 at 11:45