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

5
  • 1
    are you trying to access this identifier from different module or the same one? what do you mean under 'global' in this case? Commented Dec 3, 2018 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 Commented Dec 3, 2018 at 10:53
  • Module variables are not global, that's why you cannot access them as properties at all. Commented Dec 3, 2018 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 Commented Dec 3, 2018 at 11:37
  • I guess i could wrap them in an object... that would work. Commented Dec 3, 2018 at 11:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.