Code using function from another file without import / export


#1

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-requests/lessons/requests-ii/exercises/async-get-requests-iii

In this exercise, the main.js file uses the function renderResponse from the helperFunctions.js file, and it works. But there is no import / export statements in any of these files.

So, how can a file get a function from another file without import / export statements?renderresponse


#2

This project uses a webpage that imports scripts. Any loaded later can call the functions in the earlier loaded script. The reason being is that they occupy the same namespace.

<script src="helperFunctions.js"></script>
<script src="main.js"></script>
totality
You might be interested in:

    1. one
    2. collectives

#3

Hi mtf.

I replicated this exercise in my environment and inserted the main.js script before the helperFunctions.js script on the HTML. So, in theory, the main.js file would not be able to access a function in helperFunctions.js because this latter one would not have been loaded yet. But the function worked.

Also, there is no link on your namespace explanation. So I couldn’t study this topic.

When different js files are inserted on a HTML, does this make them instantly available for each other? Like if they were a single big file?


#4

Yes, only it’s memory they all share. The order they load is not important so long as there are no calls prior to loading. If main.js is running before helperFunctions.js is loaded, and makes a call to one of the functions, it will throw an error.