How to return the value of a function as an object property es6

You can always use Object.defineProperty() to define your object with getters and setters, but this is a hacky but a correct and easy way to do the similar things, unlike Object.defineProperty() this method doesn't need to have your object defined prior to this so it can just properties in return value.

So for example let's get a mapper function, mapper functions will usually return an objects without defining them

 

const mapProvider = ({

id,

}) => ({

id

});

 

So the trick here is if you just defined your object as a function it is not going to work.

id,

}) => ({

id,

myProp: () => { return 123; }

});

If you want to use the value then you need to call Obj.myProp().

So what you need to do is as follows, you need to define the function and call it at the same time.

id,

}) => ({

id,

myProp: (() => { return 123; })()

});

It will do the trick, now when you console.log(Obj) you'll see the return value 123.