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 Object-Oriented JavaScript (2015) Practice Project User Interface Code

Linying Wang
Linying Wang
1,479 Points

Why I don't need to pass in any parameters for QuizUI?

In QuizUI code, it directly calls quiz.hasEnded() without having to pass a parameter. And in App.js, we didn't pass in any quiz object to QuizUI either, so how does it know which quiz object to look for?

2 Answers

Hi Linying Wang,

The answer to your question lies in the scoping of variables. There are 2 scopes 1.local within function and 2..global when the variables are defined outside the functions.

If you remember in app.js we use new Quiz(); to create an instance of Quiz but unlike java we did not do any import. The interpreter was able to resolve the Quiz keyword because of global scoping. In the same way we are not passing quiz object, it has a global scope as in global variables need not be passed over. The quiz defined in app.js is the one being accessed in the Quiz_UI file. However your application would stop working if you changed the name of global variable quiz in app.js.

Hope it helps, I too had the the same doubts and did a little bit of browsing on Scoping in JS. :D

David Bath
David Bath
25,940 Points

The function hasEnded(), which is a method on the Quiz object prototype, references the "this" object, which refers to the specific instance of quiz. When you call quiz.hasEnded() "quiz" is the specific instance. So nothing needs to be passed in, it already knows which quiz.

Linying Wang
Linying Wang
1,479 Points

Yes, this part I understand. But I'm not asking this. I mean when you call quiz.hasEnded() in the QuizUI, how does it know which quiz object it should refer to? I mean, the "quiz" could also be named something else in my app.js.