Scoping issue with prototypes and async.forEach
I have a scope issue that I don't understand.
I have this object with some methods:
FileInfo = (file) -> @name = path.basename(file.path); FileInfo::uploadImage = (filename, callback) -> FileInfo::handleImage = (version, callback) -> # Here I would like to call uploadImage
I am calling handleImage from an async.forEach loop as:
async.forEach options, fileInfo.handleImage, (err) -
I would like to call uploadImage from within handleImage but I get TypeError: Object # has no method 'uploadImage'
I have tried, inside of handleImage, the following:
as well as:
that = this that.uploadImage
If I call Fileinfo.handleImage outside of the forEach loop it works fine with either this or that.
fileInfo is an instance of
It is losing the
this binding (that is, the value of
this when that function is executing, not to be confused with the execution context, see comments) that you expect, because you don't immediately invoke it, just pass a reference to it.