Reputation: 43123
I have an object literal that I'm using to group methods. I'd like to be able to call a whole group of methods easily, like so:
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"
When I try this it doesn't seem to work. What am I missing / how should you go about looping through a group of methods like this?
Upvotes: 1
Views: 100
Reputation: 159905
for ... in
compiles down to a for
loop that assumes you are looping over an array - use for own ... of
instead:
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"
Upvotes: 1