Assume I have this example file called import.js
var self;
function Test(a,b){
this.a = a;
this.b = b;
self = this;
}
Test.prototype.run = function(){
console.log(self.a, self.b)
}
module.exports = Test
When I require the file and create one new 'object' everything works perfectly, but when I create a 2nd object, they both have access to self, only the latter one works.
var Test = require('./import.js');
var one = new Test(1,2);
one.run()
1 2
var two = new Test(3,4);
two.run()
3 4
one.run()
3 4
Is there a way to re-require the file such that it creates separate scopes?
Putting it as two different variables doesn't work,
var Test1 = require('./import')
var Test2 = require('./import')
var one = new Test1(1,2);
var two = new Test2(3,4);
one.run()
3 4
But duplicating the file does exactly what I am looking for..
var Test1 = require('./import1');
var Test2 = require('./import2');
var one = new Test1(1,2);
var two = new Test2(3,4);
one.run();
1 2
Yes re-writing self
into this
would work but, But is this possible without modifying the import.js file, or duplicating it?