-1

I want to write a function which returns an object that will contain a property based on a parameter of the function.

Something like

var my_object_creator = function(prop, value) {
    return { prop: value }
}

But the value of the property should not be "prop", but to evaluate the value of prop, and this is the property name.

I know I can do it like this

 var my_object_creator = function(prop, value) {
    var t =  {};
    t[prop] = value;
    return t;
}

But I'm wondering whether there's a more elegant way.

Thanks

Yossi Vainshtein
  • 3,845
  • 4
  • 23
  • 39

1 Answers1

2

In ES2015:

return { [prop]: value };
Pointy
  • 405,095
  • 59
  • 585
  • 614