2

I am extremely weak with javascript due to its lax syntax but very punishing special characters meaning.

In react-native-navigation tutorial there is this snippet

static navigationOptions = ({ navigation }) => {
   const {state, setParams} = navigation;
   const isInfo = state.params.mode === 'info';
   const {user} = state.params;
   return {
     title: isInfo ? `${user}'s Contact Info` : `Chat with 
 ${state.params.user}`,
     headerRight: (
       <Button
         title={isInfo ? 'Done' : `${user}'s info`}
         onPress={() => setParams({ mode: isInfo ? 'none' : 'info'})}
       />
     ),
   };
 };

Originally, I mistakenly type the third line as this: const {isInfo} = state.params.mode === 'info';

and my code doesn't work.

What is the difference with: const isInfo = state.params.mode === 'info';

since the next line, there is curly brace wrapping {user}

it is very confusing for me, but these kind of minor thing is notoriously difficult to Google, so sorry and thanks in advance!

Zennichimaro
  • 5,236
  • 6
  • 54
  • 78
  • 2
    Please check this ref link: https://stackoverflow.com/questions/26999820/javascript-object-bracket-notation-navigation-on-left-side-of-assign – Jigar Shah Sep 15 '17 at 09:15

3 Answers3

12

Essentially curly braces like you've mentioned are Objects in javascript.

So making something like this in javascript

const user = {
    name: 'bob',
    age: 23,
};

Is making a user Object which you can use like this: user.name which will return bob.

With ES6 you're capable of making Objects from other Objects.

const {user} = state.params;
//user will be state.params.user

The above is basically pulling the property user from the object state.params into a new variable. Really all they're doing is making it so you don't have to write state.params.user each time and rather you can write user.

There's some other cool stuff you can do with this above technique. You can 'merge' arrays and objects into new constants which is pretty cool.

const test = {
    ...user,
    anotherProperty: 'value',
};

With react and redux (if you're using it) you'll see a lot of this: Object.assign({}, state, {}); which is used to create a new object with the previous properties of the state overwritten with the new state (because redux requires a new object). This is kind of the same as using {...state, ...newState}.

Please someone remind me what this ...Object method is called?

This line const isInfo = state.params.mode === 'info'; is a shorthand for declaring a bool. isInfo will be either true or false depending on whether state.params.mode === 'info'.

To translate the above into C++ for you

if (state.params.mode === 'info') {
    bool isInfo = true;
else {
    bool isInfo = false;
}

I can't remember if there is a similar Object array in C++ as in JavaScript bit think of Objects in JavaScript as Arrays with defined keys. That way the above {...state, ...newState} is like an 'override' of keys. So

int y = [1,2,3];
int x = [3,2,1];


for (i=0;i<=2;i++) {
    y[i] = x[i];
}

Something like that I think?

bashleigh
  • 8,813
  • 5
  • 29
  • 49
  • I see! This is a very clear explanation! Thank you very much! – Zennichimaro Sep 15 '17 at 09:21
  • 1
    @Zennichimaro I'm not very good with the name of things as you can tell ^^^ been thrown into this ES6 env so I know how you feel! (y) – bashleigh Sep 15 '17 at 09:25
  • lol, yeah.!! There is a whole lots of things I see in javascript that I don't understand but can't Google!! (and yes, I use redux!!) oh no... I don't get your 'advanced' stuff as well, hope someone reminds you, lol – Zennichimaro Sep 15 '17 at 09:26
  • lol, worse thing is I'm from c, c++ background where everything is pretty rigid and there is only one way of doing things (no shorthand), hahaha – Zennichimaro Sep 15 '17 at 09:28
  • @Zennichimaro haha :p what languages are you used to? I'll try translating? Essentially it's making a new object from 2 other objects. if `state = {param: '1'}` and `newState = {param: '2'}` then `return {...state, ...newState}` will return an Object `{param: '2'}` – bashleigh Sep 15 '17 at 09:29
  • @Zennichimaro I tried to translate this into C++ for you. Think it's correct? – bashleigh Sep 15 '17 at 09:37
  • I see! I think it is exactly like `Dictionary.Merge` in csharp, you will get a new `Dictionary` that contain the values combined from both `state` and `newState` right? – Zennichimaro Sep 15 '17 at 09:45
  • 1
    No idea! lol but yea it's like the combination of both Objects where newState's properties are overriding the state properties :) hope this helps! – bashleigh Sep 15 '17 at 09:47
4

In ES6 you can deconstruct objects into different variables. You can read more about it here

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

bennygenel
  • 23,896
  • 6
  • 65
  • 78
4

This is the ES6 syntax, The expression const {user} = state.params; is equal to const user = state.params.user; and the const {isInfo} = state.params.mode === 'info'; will result in {isInfo: undefined}. For more information you can see here.

Vahid Boreiri
  • 3,418
  • 1
  • 19
  • 34