CoffeeScript newbie here. I'm having a weird problem with the scope of a function that I've pushed onto an array and then executed within the class's member function. Basically, it seems like this isn't getting set correctly.
class TestClass
constructor: ->
@functions = [] # my array of functions
@member = "hello there! come find me!"
update: =>
func() for func in @functions
testClass = new TestClass
testClass.functions.push( ->
str = "could i find @member? " + @member? + "; this's keys: " + Object.keys(this)
console.log(str)
)
testClass.update()
The result? Strangely enough, it's:
could i find @member? false; this's keys:
top,location,window,external,chrome,v8Locale,document,$,jQuery,CoffeeScript
It seems like the context in which the function is being called is wrong. I thought that by pushing a skinny-arrow function onto my array, when that function is called it would adopt the context in which it is called (update, where this is the testClass)
Everything works just fine if I do:
update: =>
func.call(this) for func in @functions
but that doesn't seem very CoffeeScript idiomatic.