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:
this.uploadImage
as well as:
that = this
that.uploadImage
Neither work.
If I call fileInfo.handleImage outside of the forEach loop it works fine with either this or that.
Change
fileInfo.handleImagetofileInfo.handleImage.bind(fileInfo)(assumingfileInfois an instance ofFileInfo).It is losing the
thisbinding (that is, the value ofthiswhen 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.