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
haniyahya
3,275 PointsDon't we have to import Token.js before using "new Token()"?
Or did Ashley just skip it for now?
Thanks!
1 Answer
Alexander Davison
65,469 PointsThis 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:
const myNumber = 12;
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. ;)

haniyahya
3,275 Pointshaniyahya
3,275 PointsYou're right, Alexander! The classes files are indeed imported in the HTML
I didn't check there before I ask, my bad.
Thank you!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsYou're very welcome :)