1

I know with browser-run JavaScript, I could use window[varName]=value; to set global variables. I seem to remember there being a function to accomplish this in Node JS, but I'm not sure what it is.

If it helps, I'm aiming to set all the properties of an object as separate own variables.

Valkyrie
  • 841
  • 1
  • 9
  • 22
  • 6
    There is a way to do this in Node, using the global object, but you shouldn't, you should find another way to do what it is you're doing, without globals. – adeneo Aug 10 '16 at 19:10
  • 1
    The "other" way mentioned by @adeneo is to define a module which exports an object. Just import that object wherever you need it. **my-data.js** `module.exports = { foo: "bar" };`. **your-app.js** `const myData = require('./my-data'); myData.foo === "bar";` – Ryan Wheale Aug 10 '16 at 19:26

1 Answers1

0

In Node.js the global variables are stored in the global object, I think...

Then it'd be so:

global[varName] = value;
// or...
global.varName = value;

I think you're confusing something. Maybe you want to initialize a object constructor expression.

{ property: "string, or anything else" } 
// this expression returns a object that can be assigned
// everywhere, but when assigned, turns a reference

If you want to get/set properties in this object you must do the same thing you were doing before, index the object with the keys [...] or ., then you can optionally assign (set) the object's property with =, else it'll be returned.