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 trialRichard Hope
25,237 PointsHow are the quizUI functions able to access the instance of the quiz without passing the quiz as an argument to them?
In many of the quizUI function the instance of the quiz is accessed within the function without passing the quiz instance to the function as an argument ie.
displayQuestion: function() {
var question = quiz.getCurrentQuestion().question;
this.populateWithHTML("question", question);
}
In an app I've been working on I've had to change the above to something along the lines of:
displayQuestion: function(quiz) {
var question = quiz.getCurrentQuestion().question;
this.populateWithHTML("question", question);
}
where quiz is a variable that declared as follows:
var quiz = new Quiz(questions);
Although my app is completely different with different functions etc., if I try and access a variable from inside a UI function it does not work. I always have to pass the variable as an argument. Can anyone suggest why I might be having this issue?
1 Answer
Ullas Savkoor
6,028 PointsThe answer lies in scoping of variables.
The quiz variable defined inside app.js is global variable in scope and need not be passed around. I suspect the reason you were getting the error in your application was because the quiz variable might have been defined inside a function making it a local variable.