Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this JavaScript syntax: {Ci, CC}? [duplicate]

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.

like image 943
Bialecki Avatar asked Feb 14 '12 04:02

Bialecki


People also ask

What is JavaScript syntax?

It is an open-source and cross-platform language. JavaScript syntax refers to the set of rules that determines how JavaScript programs are constructed: // Variable declaration var c, d, e; // Assign value to the variable c = 5; // Computer value of variables d = c; e = c/d;

Which object does the this keyword refer to in JavaScript?

In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

What is a computation in JavaScript?

The computation is called an evaluation. The values can be of various types, such as numbers and strings. JavaScript keywords are used to identify actions to be performed. In these examples, using var or let will produce the same result. You will learn more about var and let later in this tutorial. Not all JavaScript statements are "executed".

What is the difference between JavaScript identifiers and comments?

Not all JavaScript statements are "executed". Code after double slashes // or between /* and */ is treated as a comment. You will learn more about comments in a later chapter. Identifiers are names. In JavaScript, identifiers are used to name variables (and keywords, and functions, and labels).


2 Answers

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; 
like image 185
Domenic Avatar answered Sep 28 '22 07:09

Domenic


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.

like image 37
Bill Avatar answered Sep 28 '22 09:09

Bill