I have variables in app.js
:
var G = {};
module.exports = G;
var DATA = G.DATA = 'DATA';
var F1 = G.F1 = function(val)
{
return val;
};
In this manner, I can export variables under the object G
, and at the same time, can access the variable directly writing DATA
without G.
prefix.
So far so good.
Now, I want to run a test for app.js
in test.js
:
var G = require('./app.js');
console.log(G.DATA); // -> DATA
This works, but I also want to access the variable directly writing DATA
without G.
prefix like console.log(DATA); // -> DATA
Surely, I could do like
var DATA = G.DATA;
for every variables(property) export&required module G
object, but obviously it's a tedious process to add every variable to the test file manually to correspond the G
objects.
Is there any way to do this automatically?
So far, I'm pessmistic since
JS function
encloses var
in the own scope, so in theory there's no way to have a helper function to var
for every object property.
Thanks.
PS. I would like to avoid any eval
or VM
of node solution. I have tried them in past, and too much problems.