0

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?

nietonfir
  • 4,797
  • 6
  • 31
  • 43

2 Answers2

3

It's destructuring assignment. It's equivalent to:

const features = answers.features;

It was introduced in ES2015.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you very much. Will accept as soon as the timeout has passed… ;-) – nietonfir Jun 06 '17 at 13:41
  • @nietonfir: No worries. BTW, no idea what I was thinking, object destructuring was in ES2015 along with array destructuring. (Brain a bit fuzzy today.) *(Edit: Ah, I know what I was thinking of: [Spread and rest properties](https://github.com/tc39/proposal-object-rest-spread), which unlike spread and rest for arrays, are still just a proposal.)* – T.J. Crowder Jun 06 '17 at 13:43
1

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.

Alessandro
  • 4,382
  • 8
  • 36
  • 70