Reputation:
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.
Upvotes: 2
Views: 5730
Reputation: 664559
I could assign a local variables for every 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.
No, that's how it is supposed to work. You - and only you - are in charge of what local variables exist in your module scope. No changes in the "export variables" of an included module should break your code.
Accessing properties on the imported module (with a self-chosen name) is the way to go. This is quite equivalent to Python's import app
or import app as g
.
If you want some particular properties as local variables, you will usually choose them manually, as in Python's from app import DATA, F1
. In JS, you will need a multiple var
statement like the one you've shown in your question. However, there is a syntax feature called destructuring assignment which will make this more fluid. You can use this in JavaScript 1.7+ (Gecko), CoffeeScript, or EcmaScript 6:
var {DATA, F1} = require("app.js");
Is there any way to do this automatically?
Yes and No. You should not do this, but you can - just like Python's frowned-upon from app import *
. To cite what they say, which is equally true for JavaScript:
[It] introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.
Note that in general the practice of importing
*
from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
In JavaScript, you can[1] use the with
-statement:
with (require("app.js")) {
…
}
[1]: Not in ES5.1 strict mode, though - which is recommended for optimisation
Upvotes: 8