fac18 / week6-coda-squall

https://coda-squall.herokuapp.com/
0 stars 0 forks source link

Variables in the global scope #58

Open bobbysebolao opened 4 years ago

bobbysebolao commented 4 years ago

Nice job putting your request.js script tag and the main.js script tag in index.html in the right order so that the backendCall function can be accessed from main.js:

    <script src="public/js/request.js"></script>
    <script src="public/js/main.js"></script>
  </body>

If you're going to be adding multiple scripts to html files in future projects, you should look into using ES6 modules. When you turn a script tag into an ES6 module by adding type=module to it, you prevent the variables from that file from being added to the global scope:

    <script type="module" src="public/js/request.js"></script>
    <script type="module" src="public/js/main.js"></script>
  </body>

You'd access the backendCall variable in main.js files by exporting it from request.js:

export const backendCall = ...

and then importing it at the top of main.js:

import backendCall from 'request.js';