0

We are using some new ECMAScript stuff so I don't know if that's what's going on. Basically I do:

var key = "foo";
var obj = { key: "abc" };

and it created and obj.key property instead of an obj.foo property.

enter image description here

1 Answers1

2

That's a property name, not an expression that can be a variable.

In ES6, you can make it an expression:

var obj = { [key]: "abc" };
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Whoa dude. That worked. I am completely confused about what just happened though. What do the brackets do? –  Jun 06 '16 at 20:35
  • 1
    @dolphonebubleine: It makes the interpreter evaluate the character sequence `key` instead of taking it literally. – Felix Kling Jun 06 '16 at 20:40
  • So it is an ecmascript thing? Is there a name for this type of behavior so I can read more about it? edit - nm. Just realized what is going on. duh. It's creating a literal and not using my key variable. –  Jun 06 '16 at 20:52
  • 1
    @dolphonebubleine: The ES6 feature is called "computed property names". – Bergi Jun 06 '16 at 21:06