impulsgraw
impulsgraw

Reputation: 897

ES6 Is there any way to assign the same value for two and more object properties using curly braces definition?

I need to assign the same anonymous arrow function to two different properties of the same object, which is defined with curly braces. E.g. now I have:

let testObject = {a: (a) => a+1, b: (a) => a+1};

And I want something like (pseudo-code):

let testObject = {a: b: (a) => a+1};

I actually know how to solve that problem (that is not a problem). But if there could be some way to assign the same intermediate (neither the variables, nor the primitives) values to two or more object properties like in a pseudo-code above, that would be very useful, isn't it?

Upvotes: 0

Views: 93

Answers (2)

dulebov.artem
dulebov.artem

Reputation: 733

let testObject = (fn => ({a: fn, b: fn}))(a => a+1);

Upvotes: 2

TimoStaudinger
TimoStaudinger

Reputation: 42460

No, not in the way you describe.


You could assign the function to a variable, but then it's not anonymous anymore:

const myFunc = a => a + 1;
const testObject = {a: myFunc, b: myFunc};

Upvotes: 2

Related Questions