8

I have a module script, in which I import some classes and initialize my app. But module scipts declare var myVar = "my"; in module context, what makes init variables unaccessible to main app code. But I don't know how to declare global variable in module script.

4
  • Possible duplicate of Define global variable in a JavaScript function Commented Feb 4, 2018 at 19:17
  • 3
    @ObsidianAge - That answer does not answer in the context of modules which this question is about so that is not a duplicate of this. Commented Feb 4, 2018 at 19:19
  • In modules, you probably should NOT be using a global variable. That's part of the reason for the module design in the first place. You should be exporting and importing variables that you wish to share. Commented Feb 4, 2018 at 19:20
  • You'd have to show us the relevant code and tell us what environment you're running it in for us to offer you options. What you're describing is just learning how to properly design and code with modules (which you might want to do some more reading about). It requires the appropriate structuring of the code and the right imports and exports. Commented Feb 4, 2018 at 20:04

1 Answer 1

13

Instead of var myVar = "my"; use window.myVar = "my";

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

1 Comment

It was one year ago, and I didn't understand what modules are for (and anyway I don't use modules a lot now). But as this is technically proper answer, accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.