2

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.

  • 5
    That's generally a bad idea. – SLaks Feb 04 '14 at 20:46
  • 1
    Please explain why you want to access those variables directly, so we can suggest a better approach. – Jan Misker Feb 04 '14 at 20:55
  • Ok, my project is basically a interpreter on top of JS, like `coffee-script`, and I want to have a variable `DATA` as `DATA` not `G.DATA` in the testing process. So far I have a test code in the same `app.js` file, but if possible I want to export the variables in a Object (in this case `G`) to a separate file - `test.js`. –  Feb 04 '14 at 21:00
  • There are no variables in an object, only properties, which are quite different things. Still you could do `for (k in G) { console.log(k,G[k]); }` in your test.js (if your test is to display the values of the properties of G). – Jan Misker Feb 04 '14 at 21:39
  • Thank you. `for in` sounds a good way to obtain properties, however, as I mentioned earlier, I would like to have a variables having a corresponding name for each property . –  Feb 04 '14 at 21:44
  • 1
    Bad idea, but good question :-) – Bergi Feb 04 '14 at 21:44

1 Answers1

7

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

Bergi
  • 630,263
  • 148
  • 957
  • 1,375