-1

It is that situation, when you get some code, which works, but you don't know how. What does this declaration method do?

const { actions: { createRole, updateRole } = {} } = props;
Federico Perez
  • 976
  • 1
  • 13
  • 34
Vic
  • 29
  • 4
  • 1
    This is called [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) – Sterling Archer May 22 '19 at 18:49
  • The destructuring is pointed out. But what does the inner '= {}' do? It's probably some sort of default assignment, but I'm not sure. – Peter Cheng May 22 '19 at 19:15
  • I have since figured it out, it is to prevent a typeError if props.actions is undefined, it will assign an empty object to call createRole on. – Peter Cheng May 22 '19 at 19:18

1 Answers1

-1

The code uses destructing for an nested object. The following example might help for understanding this new JavaScript syntax (has been introduced with ES6):

const user = {
  id: 339,
  name: 'Fred',
  age: 42,
  education: {
    degree: 'Masters'
  }
};
const {education: {degree}} = user;
console.log(degree); //prints: Masters

I would recommend the following resource for further examples: https://medium.com/@pyrolistical/destructuring-nested-objects-9dabdd01a3b8

Simon Thiel
  • 2,888
  • 6
  • 24
  • 46