简体   繁体   中英

How do I avoid adding JavaScript ES6 module exports to global scope for use in normal script blocks?

I've so far managed to avoid hardcore JavaScript programming but it looks like that is coming to an end. In gearing up for a major new project, I'm trying to get my head around best practices in 2018. I've been reading a lot about different module approaches and have made use of some like Dojo's AMD-based modules. In thinking about how to design my own module architecture, it seems like ES6 is the right way to go for new development. So far I understand the import/export syntax and have gotten some simple modules to work. But my current technique for loading the modules back in HTML and using them just "feels wrong". Let me boil down what I have working so far.

Module foo.js

import (bar} from '/Scripts/bar.js'

export function foo() {
    bar();
    console.log("I'm in foo");
}

Module bar.js

export function bar() {
    console.log("I'm in bar");
}

index.html

<head>
    <script type="module">
        import {foo} from "/Scripts/foo.js"
        window.foo = foo;   // save for later but "feels wrong"
   </script>
</head>

<body>
    <script>
        // foo();      // doesn't work - foo by itself is only defined in the module scope
        window.foo();  // unless "stashed" somewhere else
    </script>
</body>

Maybe this is proper technique (and I found an older question that did basically the same as me ), but it still doesn't feel like I should be polluting the global namespace like this. Assuming that's bad, what else should I be doing?

If you want the function available globally, the only way to achieve that is to put it on the global scope (AKA the window object).

A little bit nicer way to put things on the global scope, however, is to have some sort of globals object that contains all of your globals, rather than putting the function/property names directly on the window object which can potentially cause conflicts with other third-party libraries.

So you would say globals.foo() instead of window.foo()

A function can be accessed everywhere by re-importing it to many modules.

You only need to add objects to global scope where state is maintained and required across modules.

  1. create file ajax.js: make class, example Ajax, end that class with export Ajax.
  2. create a main.js file, type=module
  3. put in main.js: Ajax class with import { Ajax } from "ajax.js"
  4. create in main.js: a new class with var ajax = new Ajax();
  5. then put that variable in a window object: window.ajax = ajax.
  6. now you are ready and can use that ajax object everywhere in your application.
  7. if you bundle all your code in classes and put them in same named files you will have a very modularized application,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM