Variable declaration in reactjs documentation [duplicate] - javascript

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;

Related

I want to know the const declaration type of js [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
What is destructuring assignment and its uses?
(3 answers)
Closed 3 years ago.
I know how to declare const variable like const a = 1;
but I dont get what is this
const { app, BrowserWindow } = require('electron')
can anyone explain me?
That’s a destructuring assignment.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
It lets you make shortcuts to members of an array or structure.

Is this a new way to extend an object in JavaScript? [duplicate]

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.

const keyword usage in Javascript [duplicate]

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"

What is this js syntax called? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 6 years ago.
I'm not sure if there is a specific name for this way to pull object properties into their own variable? If there is a name, does anyone know?
var object = {
something: 'a string',
another: 'another string',
}
var { something, another } = object;
This is called object destructuring.
Object destructuring, read up on it here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
A lot of this new syntax isn't fully implemented in all js engines, so be careful when using it!
If you want to learn more about it, checkout this tutorial:
https://www.eventbrite.com/engineering/learning-es6-destructuring/

what "var { someName } = require('something')" means? [duplicate]

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 }.

Categories

Resources