This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 5 years ago.
While upgrading a private yeoman generator I stumpled upon this statement while looking through the official generator-webapp:
const { features } = answers;
I wasn't able to find anything about this, aside from the fact that it only works on node >=6.
What does this statement do? Where is this defined?
It's destructuring assignment. It's equivalent to:
const features = answers.features;
It was introduced in ES2015.
This:
const { features } = answers;
Is the shorthand of this:
const features = answers.features;
You could also declare many variables in a single line, see following please:
var answers = {"features": "test"};
const { features } = answers;
console.log(features);
var longObj = {"attr1": "val1", "attr2" : "val2"};
const { attr1, attr2 } = longObj;
console.log(attr1, attr2);
I hope it was clear. Bye.
Related
This question already has answers here:
javascript es6 array feature [...data, 0] "spread operator"
(2 answers)
Closed 2 years ago.
let tempProducts = [];
storeProducts.forEach((item) => {
const singleItem = { ...item };
tempProducts = [...tempProducts, singleItem]; <----- This Line
});
What does the above indicated line mean? It looks like new ES6 syntax, but unlike anything I have seen before. Can anyone explain this to me please?
It creates a new array which consists of the items from tempProducts and the singleItem.
It has the same result as
tempProducts.concat(singleItem);
The (quite modern) syntax with the three dots is called 'spreading', the ... is the spread operator.
This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 5 years ago.
I have recently come across this line of code.
const { Bar, Data: { Selectors } } = require('some-module');
Can someone please tell me what does Data: { Selectors } do in this piece of code? I understand that it is importing some module in my codebase, just confused about the {Selectors} part.
Also, if I want to write it using import syntax what would be the equivalent code for the same?
e.g : import Bar from "some-module"
It doesn't really have to do with modules (syntax is the same for import or require: import {Bar} from "some-module" ), it's just a destructuring assignment creating two variables, Bar and Selector.
Here's another example along with many other ES6 features http://es6-features.org/#ObjectMatchingDeepMatching
const obj = {Bar: 1, Data: { Selectors: [1,2]}};
const { Bar, Data: { Selectors } } = obj;
// This is another ES6 feature, same as saying
// console.log({Bar: Bar})
console.log({Bar});
console.log({Selectors});
The above is the same as saying
const Bar = obj.Bar;
const Selectors = obj.Data.Selectors
This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
ES6 Object Destructuring Default Parameters
(1 answer)
Closed 6 years ago.
I was reading the source for a MeteorJS package (this one) when I came across this:
const {
getMeteorData,
pure = true,
} = expandedOptions;
I did some research to figure out what this code would do, but couldn't find any other instance of const being used like this, or any documentation to clarify it for me.
Any hints, please?
It is using es6 to unpack (or destructure) the values inside the expandedOptions object.
const val = {a:1, b:"hello"};
const {
a,
b
} = val;
console.log(a); //1
console.log(b); //"hello"
This question already has answers here:
What is this JavaScript syntax: {Ci, CC}? [duplicate]
(2 answers)
Closed 7 years ago.
I was reading a MDN docs about firefox addons and I saw some syntax I didn't understand.
In one of the examples they say:
var { ToggleButton } = require('sdk/ui/button/toggle');
var panels = require("sdk/panel");
var self = require("sdk/self");
var button = ToggleButton({
...
});
Why is the variable name inside the braces: var { ToggleButton } =?
What is happening here?
You are witnessing a usage of the new ES 2015 feature called destructuring.
Refer: https://github.com/lukehoban/es6features#destructuring
Destructuring with Objects is supported in Firefox:
https://kangax.github.io/compat-table/es6/#destructuring_with_objects
Update:
Here is the proof.
The ToggleButton module exports ToggleButton object:
https://github.com/mozilla/addon-sdk/blob/master/lib/sdk/ui/button/toggle.js#L79
And as per the ES 2015 destructuring rules, it gets destructured properly into { ToggleButton }.
This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 7 years ago.
react.js documentation has following variable declarations:
var { Image, StyleSheet, Text, View } = React;
Could you tell what does it meant? Thank you.
This is the destructuring syntax, which is part of ES6.
It's a feature of ES6 called destructuring. Essentially the same as writing:
var Image = React.Image;
var StyleSheet = React.StyleSheet;
var Text = React.Text;
var View = React.View;