in CoffeeScript, how can I use a variable as a key in a hash?
问题 eg: So: foo = "asdf" {foo: "bar"} eval foo # how do I get {"asdf": "bar"} ? # this will throw parse error: {(eval foo): "bar"} This is a simple syntax question: how do I get CoffeeScript to construct a hash dynamically, rather than doing it by hand? 回答1: For anyone that finds this question in the future, as of CoffeeScript 1.9.1 interpolated object literal keys are supported! The syntax looks like this: myObject = a: 1 "#{ 1 + 2 }": 3 See https://github.com/jashkenas/coffeescript/commit