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

Ryan Libed
Ryan Libed
7,386 Points

How does one function access another functions prototype?

I'm confused on how the guess method from the Quiz prototype is able to call the isCorrectAnswer() on the Question prototype?

Quiz.prototype.guess = function  (answer) {
  if (this.getCurrentQuestion().isCorrectAnswer(answer)) {
    return this.score++;
  }
  return this.currentQuestionIndex++;
}
Question.prototype.isCorrectAnswer = function (answer) {
  return this.choice === answer;
}

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

It's not calling the method on Question's prototype, but instead on an instance of the Question object. The code isn't included in your question, so I'm guessing based on the names, but I'm guessing that Quiz also has a method named getCurrentQuestion that returns an instance of the Question object. It immediately calls isCorrectAnswer on that object to check if the passed-in answer is the correct answer to the current question. That all being said, it's important to note that every property of an object is accessible to every other object/function of the same or a more specific scope, so you can always read/write the prototype of any object you can access