lonewarrior556 lonewarrior556 -4 years ago 89
Javascript Question

Is it possible to re-require a file to get different scope?

Assume I have this 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, however, when I create 2 objects, 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 anyway to re-require the file such that it creates a new separate scope?

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);
two.one()
3 4


However duplicating the file works exactly how I would like it to..

var Test1 = require('./import1')
var Test2 = require('./import2')
var one = new Test1(1,2);
var two = new Test2(3,4);
two.one()
1 2


Is it possible to do this somehow without modifying the import.js file, or duplicating it?

Edit:



Yes re-writing
self
into
this
would work but it's not what I'm asking. I would like to know if it's possible to reestablish a different scope.
A real import.js file can contain robust objects with recursive calls, that get passed through other functions. Having a top level
self
, relieves you of any need or worry about scope and binding.
Assuming you have a complex file,and it always gets required once, but now for testing or whatever you would like to import it twice... What are the other options to recoding your work?

Answer Source

Two ways to make it work....

(1) Deleting Cache

How to remove module after "require" in node.js?

var Test1 = require('./import.js');
delete require.cache[require.resolve('./import.js')]
var Test2 = require('./import.js');

var one = new Test1(1,2);
var two = new Test2(3,4);

one.run()
1 2
two.run()
3 4

Doesn't even look that messy, although it's grossly inefficient and would get costly very fast to write code this way...


(2) Using function Scope

Because require reads the file and then runs it,

var Test = require('./test.js');

is equivelent to

var Test = eval( fs.readFileSync('./test.js', 'utf8') );

So, if instead of using require, you read the file you can establish new scopes inside of functions:

var fs = require('fs');
var File = fs.readFileSync('./import.js', 'utf8');
var Test1, Test2;
(function(){ Test1 = eval(File); })();
(function(){ Test2 = eval(File); })(); 

The self inside the file would now be stored inside the function scope you created. so once again:

var one = new Test1(1,2);
var two = new Test2(3,4);

one.run()
1 2
two.run()
3 4

Slightly messier, but far faster then deleting the cache and re-reading the file every time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download