Reputation: 14896
for a in [1,2,3]
$('body').click (x) =>
alert a
It alerts 3 three times when I click the body. I would like it to alert 1, 2, and 3.
Upvotes: 1
Views: 60
Reputation: 48167
I screwed up the syntax the first time, but got it right this time:
You need to create a closure around the function and call it immediately (much like you would in Javascript). Coffeescript even gives you a nice syntax to do that for you... the do keyword:
for a in [1,2,3]
do (a) ->
$('body').click (x) =>
alert a
Upvotes: 5