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
枚举键而不是值)。啊,我明白了。我不知道你可以像访问散列属性一样调用一个方法,但当然这就是全部,为什么不呢!谢谢