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

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

eg:

\"wtf\"

So:

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

# how do I g         


        
6条回答
  •  自闭症患者
    2021-01-07 16:33

    Why are you using eval at all? You can do it exactly the same way you'd do it in JavaScript:

    foo    = 'asdf'
    h      = { }
    h[foo] = 'bar'
    

    That translates to this JavaScript:

    var foo, h;
    foo = 'asdf';
    h = {};
    h[foo] = 'bar';
    

    And the result is that h looks like {'asdf': 'bar'}.

提交回复
热议问题