0

Is it possible to access a global variable inside an es6 module using the bracket notation, basically doing something like this:

const GLOBAL = 'something'

console.log(this['GLOBAL']) // evaluates to undefined, of course

Doesn't seem to be working

U2ros
  • 243
  • 2
  • 11
  • 1
    are you trying to access this identifier from different module or the same one? what do you mean under 'global' in this case? – skyboyer Dec 03 '18 at 10:51
  • 2
    Global from where? If it's inside the same module but declared at the top then just `console.log(GLOBAL)` will work - if it's in another module then `export\import` will work - it's a bit unclear what you mean by `global` – StudioTime Dec 03 '18 at 10:53
  • Module variables are not global, that's why you cannot access them as properties at all. – Bergi Dec 03 '18 at 10:57
  • In the same module, within some function which receives a parameter, by which i would like to create a reference to the variable with bracket notation – U2ros Dec 03 '18 at 11:37
  • I guess i could wrap them in an object... that would work. – U2ros Dec 03 '18 at 11:38

1 Answers1

1

Global constants do not become part of the widnow object, unlike var. This declaration creates a constant whose scope can be either global or local to the block in which it is declared, but if left alone in the global, they do not become property of the window object.

squeekyDave
  • 918
  • 2
  • 16
  • 35