3

when I have:

const foo = 1;

I can set a shorthand for object property names

const bar = {foo}

thats the same like

const bar = {foo: foo} // bar will be {foo: 1}

is there a shorthand when I have a const with an arrow function?

const foo = () => 1

i have to set it like this, but its uncool

const bar = {foo: foo()}

I would like to do it like this or something thats shorter

const bar = {foo()}  //bar should be {foo: 1} but this will fail
Dude
  • 1,045
  • 2
  • 15
  • 36
  • 4
    No, there's no syntax which would make it shorter, but this looks like an [XY problem](http://xyproblem.info/). What are you actually trying to do? Do you have a lot of such functions? Where do they come from? – Michał Perłakowski May 04 '18 at 16:53
  • 4
    There's no such syntax. This operation isn't in demand. It's just a coincidence that a function has same name as property. – Estus Flask May 04 '18 at 16:53
  • 1
    I just would like to have the same benefit like in the const only example. there was an answer 5 seconds ago and it was exactly what I was looking for. will it come back? :D – Dude May 04 '18 at 16:59

3 Answers3

6

Looking at the specs, there's no syntax for this.

enter image description here

In your case ObjectLiteral must resolve to { PropertDefinitionList }, which must resolve to PropertyDefinition. Now what could PropertyDefinition be?

It can't be IdentifierReference - that's your first example: {f}.

CoverInitializedName doesn't really fit either. That would look like {f = AssignmentExpression} where AssignmentExpression is defined here.

PropertyName: AssignmentExpression is the conventional syntax: {f: 1}

MethodDefinitionlets you do {f(){ return 1; }};, but that again isn't what you want.

Looks like the verdict is no.

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • Imho this is a missing feature. But thanks for looking into the formal specs and clearing out why its not possible in the way I would like to do it. – Dude May 04 '18 at 17:29
1

The symmetry is already there. {foo} is equivalent to {foo: () => 1 }, i.e. an object containing a property foo which is a function. E.g. you could then call it like this if you wanted:

bar.foo();

Your request seems to be for new syntax that instead calls foo and populates bar.foo with the result. There's no such syntax. What if foo takes arguments? What if foo is asynchronous (you'd want to use await then)? How did you end up with functions with the same name as properties? Without a demonstrated use-case, it's hard to see any value in optimizing for this pattern.

Shorter isn't always more readable.

If you have a set of functions dedicated to providing the results for a set of same-named properties, you could perhaps construct something, using a mapObject helper, like this:

const mapObj = (o, f) => Object.assign({}, ...Object.keys(o).map(k => ({[k]: f(o[k])})));

const foo = () => 1;
const fum = () => 2;

let bar = mapObj({foo, fum}, f => f());

console.log(JSON.stringify(bar));
jib
  • 40,579
  • 17
  • 100
  • 158
0

Ok I forgot that there are also IIFE possible with arrow functions.

so if you like the benefit of this shorthand you can do

const foo = (() => 1)()
const bar = {foo}

downside: you need an IIFE

Dude
  • 1,045
  • 2
  • 15
  • 36
  • 3
    which is just setting foo to 1 – Luca Kiebel May 04 '18 at 17:05
  • 3
    Why not just do `const foo = 1`? – Michał Perłakowski May 04 '18 at 17:06
  • its just an example for a dummy function. the function in real life is more complex – Dude May 04 '18 at 17:06
  • 2
    @Luca well, I suppose you can have a non trivial function...but the IIFE is still unnecessary - just do the operations and assign the result to a variable, no need to make a new function just so you execute it on the same line and discard it. – VLAZ May 04 '18 at 17:07
  • 1
    The answers are as good as the question is. Why would you need IIFE in ES6 in a situation like this one? We already have blocks to create nested scopes. – Estus Flask May 04 '18 at 17:08
  • long story short: the situation is more complex in general because I have lots of dynamic objects and I was looking for a solution to avoid the `bar = {foo:foo}` problem. It will make the code more readable with a shorthand solution and makes it easier to maintanance the code when something will change from my API. the answer is not really from myself. there was a user and posted it here but the answer was deleted quickly. – Dude May 04 '18 at 17:22