Java Reference
In-Depth Information
Quiz Ninja Project
For the quiz project, we're going to replace the main play() function of the quiz with a
Game() constructor function that will create a new Game instance every time it's invoked
with the new operator. This will allow us to apply in our quiz some of the principles of OOP
covered in this chapter.
The Game() constructor function is very similar to the first part of the play() function:
function Game(quiz){
this.questions = quiz.questions;
this.phrase = quiz.question;
this.score = 0; // initialize score
update($score,this.score);
// initialize timer and set up an interval that counts
down
this.time = 20;
update($timer,this.time);
this.interval = window.setInterval(
this.countDown.bind(this) ,
1000 );
// hide button and show form
hide($start);
show($form);
// add event listener to form for when it's submitted
$form.addEventListener('click', function(event) {
event.preventDefault();
this.check(event.target.value);
}.bind(this), false);
this.chooseQuestion();
}
The main differences between the Game() constructor function and the play() function
are:
• The variables score and time become this.score and this.time as they
are properties of the Game instance.
 
Search WWH ::




Custom Search