5
I have variables in app.js
:
var G = {};
module.exports = G;
var DATA = G.DATA = 'DATA';
var F1 = G.F1 = function(val)
{
return val;
};
This way I can export variables under the object G
, and at the same time you can access the type variable directly DATA
without G
. prefix.
Now, I want to audition 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 writing variable directly DATA
without G
. Prefix as console.log(DATA); // -> DATA
Surely, I could do as:
var DATA = G.DATA;
For each (property) export variable and module required G
object, but obviously it is a tedious process to add each variable to the test file manually to match the G
object.
Is there any way to do this automatically?
So far, I’ve been pessimistic since
JS function
closes var
in the scope itself, so in theory, there is no way to have an auxiliary function to var
for each property object.
I’d like to avoid any eval
or VM
knot solution. I tried them in the past, and had too many problems.
Even if the
with
may cause bugs and has a negative effect on program performance.– luiscubal