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

后端 未结 7 1709
南旧
南旧 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:19

    As others have said, no, there's currently no syntax for interpolated key strings in object literals in CoffeeScript; but it seems at some point this feature existed! In these GitHub issues there's some discussion about it: #786 and #1731.

    It's implemented in Coco and LiveScript as:

    b = 'foo'
    a = {"#{b}": 'baz'}
    
    # Or..
    a = {(b): 'bar'}
    

提交回复
热议问题