Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Don't we have to import Token.js before using "new Token()"?

Or did Ashley just skip it for now?

Thanks!

1 Answer

This is confusing, I agree.

The HTML file "imports" both of the files, so therefore they are able to access each others' variables/classes.

This is my interpretation of how it works, if I'm wrong, please tell me (to fellow students)

Think of it like this: here's my HTML:

<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        ...
        <script src="file1.js"></script>
        <script src="file2.js"></script>
    </body>
</html>

Here's my JS:

file1.js
const myNumber = 12;
file2.js
console.log(myNumber);

Behind the scenes, the HTML reads the file and merges the JS code into the HTML:

<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        ...
        <!-- file1.js -->
        <script>
            const myNumber = 12;
        </script>

        <!-- file2.js -->
        <script>
            console.log(myNumber);
        </script>
    </body>
</html>

Now you can see how they are able to access each others' variables. Sure, they are in different <script> tags, but HTML considers them to be in the same "scope"

I hope this helps. ;)

You're right, Alexander! The classes files are indeed imported in the HTML

<script src = "js/Game.js"></script>
<script src = "js/Board.js"></script>
<script src = "js/Space.js"></script>
<script src = "js/Player.js"></script>
<script src = "js/Token.js"></script>
<script src = "js/app.js"></script>

I didn't check there before I ask, my bad.

Thank you!

You're very welcome :)