I have this:
function change( event, file ) {
console.log( "filename", file );
//It should be '_file', not 'file'.
files.clients( file, function( clientOfFile ) {
console.log( "for client:", clientOfFile );
io.sockets.socket( clientOfFile ).emit( "change" );
} );
}
client.on( "watch", function( file ) {
_file = base + file; //filename with path
files.add( _file, client.id );
fs.watch( _file, change );
} );
fs.watch passes to callback a filename without path. So I want it to get parent function argument _file. I thought I can use .call, but how to do it in callback?
Plenty of possiblitys, one is to use
Function.prototype.bind, if you don’t need to have access to the originalthis valuewithin the callback:That way you can access
_filelikewithin your callback method.
One word of caution: Be aware that you are using another anonymous function in your callback method for the callback of
files.clients.thisdoes not reference the same value within there. So if you want to access our newly passedthisreference there, you need to either invoke another.bind()call or just store an outer reference tothisin a local variable.