I have a "class" in coffee script whose instance variables I want to initialize with instance methods that return a value via a callback, but it doesn't work as I had hoped:
EventEmitter = require('events').EventEmitter
class MyClass extends EventEmitter
constructor: ->
@datamember: setDatamember()
setDatamember: ->
someFunction (response) ->
@datamember = response
@emit 'init'
getDatamember: ->
return @datamember
----
myObj = new MyClass
myObj.on 'init', ->
console.log myObj.getDatamember
The result I get suggests that "this" in setDatamember is referring to something different from what "this" refers to in the object instance. If I explicitly call myObj.setDatamember, I get the expected result, but is there any way to call on a set method -- specifically one that sets the data member via a callback -- in the constructor? I've looked through the docs, as well as various other sources of coffeescript info (e.g. this one), and I haven't found anything that touches upon this.
Try using a fat arrow for the anonymous function:
Also, you'll need to call the correct function in the constructor:
In general, avoid fat arrows on methods - the way Coffee-Script implements this does some bad things to memory usage. Also, it will rarely be necessary.
However, anonymous functions that refer to
this
will almost always need fat arrows.this
is not held in closure like normal variables, and will only be set by binding (Function.prototype.bind
) or by calling it as an object method (obj.myMethod()
will setthis
toobj
inmyMethod
).