in CoffeeScript, how can I use a variable as a key in a hash?

后端 未结 6 2088
野趣味
野趣味 2021-01-07 16:00

eg:

\"wtf\"

So:

foo = \"asdf\"
{foo: \"bar\"}
eval foo

# how do I g         


        
6条回答
  •  忘掉有多难
    2021-01-07 16:39

    CoffeeScript, like JavaScript, does not let you use expressions/variables as keys in object literals. This was support briefly, but was removed in version 0.9.6. You need to set the property after creating the object.

    foo = 'asdf'
    
    x = {}
    x[foo] = 'bar'
    alert x.asdf # Displays 'bar'
    

提交回复
热议问题