0

I can do this:

require('zappajs') ->
    @get '/':-> console.log(@response)

but when I try this

require('zappajs') ->
    @get '/': -> foo()

    foo = ->
      console.log(@response)

@response is undefined. So obviously 'this' is now out of scope. I tried using the => function definition instead of -> which is meant to pass 'this' through ... but it makes no difference. I can achieve the desired result using a @helper

require('zappajs') ->
    @get '/':-> @foo()

    @helper foo: ->
        console.log(@response)

Is that the only way of doing this?

stukennedy
  • 1,088
  • 1
  • 9
  • 25

1 Answers1

0

You're losing scope when you're calling your function like that foo(), to keep the scope, you have to call foo like that:

foo.apply(this) or foo.call(this)

adriantoine
  • 2,160
  • 1
  • 15
  • 14