Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Returning Values

Fabio Silva
Fabio Silva
5,097 Points

Packages in JavaScripts?

How does the button.onclick function know where the object dice and its method are, since they are not in the same file?

Shouldn't it be referenced by a keyword like "import"? import dice;

If we want to access the method of an object that is in a different file can we simply call it? I'm assuming these files need to be in the same folder directory? Right?

2 Answers

Niclas Valentiner
Niclas Valentiner
8,947 Points

If the file containing the referred object is included prior to the reference to the object (or method/property) then it'll work.

This is because including files effectively copy pastes the entirety of that file into the DOM so it's basically putting everything together in one big 'file'.

etern Mistolo
etern Mistolo
1,200 Points
<script src ="ui.js"> </script>
<script src ="app.js"> </script>
<script src ="jquery.js"> </script>

etc