I have two javascript files that look like:
// module1.js
var globalFunction = function(val1, val2, callback){
var localVariable1 = val1;
var localVariable2 = val2;
someAsyncFunction(function(){
callback(localVariable1, localVariable2 );
});
}
module.exports = function(val1, val2, callback){
var localVariable1 = val1;
var localVariabl2 = val2;
anotherAsyncFunction( function(){
globalFunction(localVariable1, localVariable2, callback);
});
}
and this:
function MyClass(val1, val2){
this._val1 = val1;
this._val2 = val2;
this._boundFunc = require("./module1.js").bind(this);
}
MyClass.prototype.doSomething = function(callback){
this._boundFunc(callback);
}
Now this incredibly contrieved example binds the module.exports to the class MyClass. What is happening under the hood here? Does each MyClass instance have its own copy of the module.exports function and also will it have it's own copy of the globalFunction as this is referenced in the module.exports?
My concern is that if multiple "doSomething" functions are called synchronously on two difference instances of MyClass then they can interfere and overwrite each others local variables whilst waiting on the asyncFunctions to callback. If the bind copied both the globalFunction and module.exports then I dont think Id have a problem. Thanks
this._boundFunc(callback);- the exported function you have expects 3 arguments, not just one.thiskeyword