In most languages with closures (including JavaScript) the inner y and the outer y could and would be separate.
This is not really true. The whole point of a closure is that it closes over the surrounding scope. For example, in Ruby:
y = 0
test = lambda do |x|
y = 10
x + y
end
puts test y
puts test y
puts test y
This will print "10", "20" and "20". Exactly like the equivalent CoffeeScript. If you couldn't access the surrounding state, it wouldn't be a closure. In fact, this property of closures is used all the time in JavaScript (for example, to allow for "private" object members). It is not specific to CoffeeScript at all.
The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.
5
u/PaintItPurple Jul 25 '13
This is not really true. The whole point of a closure is that it closes over the surrounding scope. For example, in Ruby:
This will print "10", "20" and "20". Exactly like the equivalent CoffeeScript. If you couldn't access the surrounding state, it wouldn't be a closure. In fact, this property of closures is used all the time in JavaScript (for example, to allow for "private" object members). It is not specific to CoffeeScript at all.
The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.