在Coffeescript的函数参数中使用哈希

时间:2014-09-25 22:13:27

标签: javascript coffeescript

目前,我正在使用CoffeeScript,我需要能够将选项传递给参数并让它们在函数中运行。

在红宝石中,我会做这样的事情:

def some_method(options = {})
  options.each do |key, value|
    puts "the #{key} key has a value of #{value}"
  end
end

some_method(hello: "world", something: "else")

我将如何在CoffeeScript中进行此操作?

2 个答案:

答案 0 :(得分:0)

你应该看看咖啡文档。

答案 1 :(得分:0)

在这里,这很简单:

some_method = (options) ->
  alert "the #{key} key has a value of #{value}" for key, value of options

some_method hello: "World", something: "else"

为了简化从红宝石到咖啡的过渡,您还可以将for循环分开:

some_method = (options) ->
  for key, value of options
    alert "the #{key} key has a value of #{value}"

some_method hello: "World", something: "else"
相关问题