Is there any way to declare a variable at "file" scope (which will be closured by CS), without initializing it? A contrived example:
init = ->
counter = 0
inc = ->
counter += 1
This won't work, because you need to declare "counter". Adding "counter = 0" to the top would make it work, but the "= 0" is unnecessary. (A more realistic example would involve something that accesses the DOM on page load - there's no way to correctly initialize it in "file" scope.)
You'll have to define it on the outer scope, as you mentioned.
counter = null
init = ->
counter = 0
inc = ->
counter += 1
If your functions where part of an object you could use @counter, like this:
obj =
init: ->
@counter = 0
inc: ->
@counter += 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With