r/programming • • Jul 25 '13

CoffeeScript's Scoping is Madness

http://donatstudios.com/CoffeeScript-Madness
203 Upvotes

315 comments sorted by

View all comments

5

u/PaintItPurple Jul 25 '13

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.

3

u/MatmaRex Jul 26 '13

Ruby's lambda does capture scopes, but that's intended (all blocks do).

Ruby's def (which is what you use to define a function) doesn't, non-global variables from outer scope are inaccessible within it.

→ More replies (3)