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.
Asked
Active
Viewed 5,304 times
6
-
Possible duplicate of [Define global variable in a JavaScript function](https://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) – Obsidian Age Feb 04 '18 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. – jfriend00 Feb 04 '18 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. – jfriend00 Feb 04 '18 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. – jfriend00 Feb 04 '18 at 20:04
1 Answers
13
Instead of var myVar = "my";
use window.myVar = "my";

Brooks Roche
- 268
- 4
- 8
-
1It 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. – Feb 20 '20 at 20:48