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).

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

I feel embarrassed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.