Is it possible to define a dynamically named property using object literal in JavaScript?

后端 未结 7 1644
南旧
南旧 2021-01-17 16:05

Consider the following

var a = {foo: \"bar\"};

Equivalent to

var a = {};
a.foo = \"bar\";

Equivalent to

7条回答
  •  囚心锁ツ
    2021-01-17 16:20

    JavaScript

    var a, b;
    (a = {})[b = 'foo'] = 'bar';
    

    CoffeeScript

    (a = {})[b = 'foo'] = 'bar'
    

提交回复
热议问题