1

I am reading this explanation in order to get a better understanding of ES6.

In the bit about Enhanced Object Literals, this is given as example code:

var obj = {
    ... (removed as not relevant) ...

    // Computed (dynamic) property names
    [ 'prop_' + (() => 42)() ]: 42
};

I understand what is happening except for the last sentence

I get that

: 42

Is the value (Number) that will be given to the property, and that

[ 'prop_' + ... ]

Is a dynamic variable name that starts with the string prop_.

However, what does this mean/do?

(() => 42)()
Davy
  • 691
  • 1
  • 7
  • 18
  • 2
    It's basically an IIFE, something like `(function() { return 42 }())` – adeneo Jan 11 '16 at 14:16
  • 1
    As for the duplicate, I feel it's not a duplicate, as my question is not what the => specfically means, but the entire sentence, including the usage of the ()'s. – Davy Jan 11 '16 at 15:07
  • And thanks @adeneo, your comment makes me understand better what happens here. – Davy Jan 11 '16 at 15:21
  • @Mortaza The mere addition of the `()` to execute the function does not make the question unique, I'd say; it's still a dump in my mind. –  Jan 11 '16 at 15:35

1 Answers1

3

(() => 42)() is a long way of writing 42 in ES6 using an arrow function.

Paul
  • 139,544
  • 27
  • 275
  • 264