Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CoffeeScript循环通过方法_Javascript_Loops_Methods_Coffeescript - Fatal编程技术网

Javascript CoffeeScript循环通过方法

Javascript CoffeeScript循环通过方法,javascript,loops,methods,coffeescript,Javascript,Loops,Methods,Coffeescript,我有一个用于分组方法的对象文本。我希望能够轻松调用整个方法组,如下所示: group = methodA: (str) -> console.log str + "from method A" methodB: (str) -> console.log str + "from method B" for method in group method "hello" # should log to console: # "hello from meth

我有一个用于分组方法的对象文本。我希望能够轻松调用整个方法组,如下所示:

group =
  methodA: (str) ->
    console.log str + "from method A"

  methodB: (str) ->
    console.log str + "from method B"

for method in group
  method "hello"

# should log to console:
# "hello from method A"
# "hello from method B"

当我尝试这个的时候,它似乎不起作用。我遗漏了什么/您应该如何循环使用一组这样的方法?

用于。。。在
中,编译为一个
for
循环,该循环假定您正在数组上循环-使用
for own。。。改为

group =
  methodA: (str) ->
    console.log str + "from method A"

  methodB: (str) ->
    console.log str + "from method B"

for own method of group
  group[method] "hello"

用于。。。在
中,编译为一个
for
循环,该循环假定您正在数组上循环-使用
for own。。。改为

group =
  methodA: (str) ->
    console.log str + "from method A"

  methodB: (str) ->
    console.log str + "from method B"

for own method of group
  group[method] "hello"

获取
类型错误:方法不是函数。如果我只是在for循环中执行
console.log方法
,它会打印
methodA,methodB
Mea culpa@Andrew-我已经修复了它(
for…of
枚举键而不是值)。啊,我明白了。我不知道你可以像访问散列属性一样调用一个方法,但当然这就是全部,为什么不呢!谢谢获取
类型错误:方法不是函数。如果我只是在for循环中执行
console.log方法
,它会打印
methodA,methodB
Mea culpa@Andrew-我已经修复了它(
for…of
枚举键而不是值)。啊,我明白了。我不知道你可以像访问散列属性一样调用一个方法,但当然这就是全部,为什么不呢!谢谢