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

Russ Tuttle
seal-mask
.a{fill-rule:evenodd;}techdegree
Russ Tuttle
Full Stack JavaScript Techdegree Student 8,909 Points

How do I call a method from a different class?

How do I access a method that comes from a different class? I am within the "Game" class now where getRandomPhrase() method was created and that works. addPhraseToDisplay method was created in a different class than I currently am in called "Phrase" and I keep getting an error when I try to call it in the "Game" class within the startGame function.

  startGame() {
  const overlay = document.getElementById("overlay");
  overlay.style.display = "none";
  this.getRandomPhrase();
  this.addPhraseToDisplay();
  }
}

1 Answer

Steven Parker
Steven Parker
231,271 Points

If this code is part of the "Game" class, then this would refer to a "Game" object. To call the other class method, you need to refer to that other class.

To make a more explicit answer possible, can you share the whole code (perhaps as a snapshot)?