0

i need a way to call variables in a file dynamic, in the front end if you create a variable outside a function its put it on the window object and then you can get it using

window['nameOfVar']

i have tried do it with this instead of window in nodejs but i got nothing.

const self  = this;
var temp = 'im dynamic'

console.log(self['temp']) // this will print undefined in nodejs

is there a way to achieve this behavior

Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
  • Is this what you're looking for? `self.temp = "im dynamic"` . In Javascript you can reference by dot-notation or access via brackets. It means the same, but if you need to iterate over a lot of properties, its far easier for bracket notation since you can hot swap out k-v pairs – Fallenreaper Mar 05 '19 at 15:19
  • `console.log(self['temp'])` this does **NOT** print `undefined` in nodejs. It returns `undefined` though, but just as in any browser. – sjahan Mar 05 '19 at 15:20
  • im looking for a way to call variables by other variables names – Amit Wagner Mar 05 '19 at 15:21
  • If you're looking for global variables, try this [link](https://stackoverflow.com/questions/5447771/node-js-global-variables) – Sven Mar 05 '19 at 15:21
  • You would then need to remove the quotes around `temp`: `self[temp]` is `self["im dynamic"]` – Luca Kiebel Mar 05 '19 at 15:22
  • Please clarify your question. – sjahan Mar 05 '19 at 15:22
  • If you want to encapsulate your variable, do it and use a proper object. Do not try to use the 'file encapsulation', it's messy. If you want to use the variable from an other file, there is the module export/import system. – Orelsanpls Mar 05 '19 at 15:24
  • This works in browsers because the `this` context is referred to `window `object,at top level. but in node ,the default context for `this` at the top level is empty and not referenced to any globals. Changing your variable definition as `this.temp = "im dynamic";` would work – jenil christo Mar 05 '19 at 15:42

1 Answers1

5

is there a way to achieve this behavior

Yes, but you don't want to use it.

What you're describing works in browsers because the top-level scope is global, and top-level globals using var create properties on the global object, which is accessible as window. It doesn't work in Node.js because by default Node.js runs your code as a module, and code at the top level of your module isn't at global scope, so using var in that code doesn't create a property on the global object (and there's no window global to access the global object with, although Node.js does have global instead — but don't use it).

Instead of doing it that long-way around, use your own object and properties:

const stuff = {
    one: "I'm one",
    two: "I'm two"
};

for (let i = 0; i < 5; ++i) {
    const name = Math.random() < 0.5 ? "one" : "two";
    console.log(stuff[name]);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    *(Made this a CW because there **must** be a useful dupetarget for it, although it depends on which part of the question you focus on. [Why `var` creates a global, how you access the global object on Node.js, why doing this with `var` and the global object isn't great and what to do instead...])* – T.J. Crowder Mar 05 '19 at 15:27
  • your answer clarify the right way to do it and its a good answer but is there (i don't care if its messy or not best practice ) way to get this variable in the module or i must set it in the global and then i didn't gain anything and sure i will prefer to us in a dedicated object like you suggested ? – Amit Wagner Mar 05 '19 at 15:50
  • @AmitWagner - Node.js has a global variable called [`global`](https://nodejs.org/api/globals.html#globals_global) that serves the same purpose that the `window` variable does on browsers: It's a reference to the global object. It's non-standard, but part of the Node.js API. So you could create properties on `global` and access them via `global[name]`. I do strongly recommend not doing that. :-) – T.J. Crowder Mar 05 '19 at 15:59