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.

Brian Prouty
Front End Web Development Techdegree Student 10,143 PointsWhat if you wanted to have the speak method be determined from the type of animal?
Say that you wanted the speak() method to be determined by the type of animal, would you be able to run an if statement like this:
class Pet {
constructor(animal, age, breed) {
this.animal = animal;
this.age = age;
this.breed = breed;
}
speak(){
if( this.animal === 'dog') {
console.log('woof');
} else if (this.animal === 'cat') {
console.log('meow');
}
}
}
Or would you run into a scoping issue?
1 Answer

KRIS NIKOLAISEN
54,664 PointsHere is a detailed article on variables and scope where he states
The this keyword is used to define object properties. Once defined in constructor, they become available for access in all class methods via the this keyword
So yes this.animal
is available to the speak()
method