0

I use CS in Rails. If I have:

foo = ->
  ...

bar = ->
  ...

-> 
  someCount = 123
  foo()
  bar()

How can I access someCount inside foo() and bar() without passing it directly as an argument?

I thought that this would require declaring someCount as a global variable. I read this and this, but I don't understand how to implement it. I tried:

root = exports ? this
root.someCount = 123

but then inside foo() I couldn't access it by either someCount (someCount is not defined) or root.someCount(root is not defined).

Community
  • 1
  • 1
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130

1 Answers1

1

You simply need to declare somecount in a scope that the other functions are also in:

somecount = null

foo = ->
  alert somecount

bar = ->
  alert somecount

-> 
  someCount = 123
  foo()
  bar()
deceze
  • 510,633
  • 85
  • 743
  • 889