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

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,893 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