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 Object-Oriented JavaScript Getters and Setters Object Interaction

Mohamad Faiz Khairi Mohd Nazri
Mohamad Faiz Khairi Mohd Nazri
5,668 Points

Why do we need the getter method for the setter method?


since the phone method has set the property of _phone with the value that was passed as the argument when called. So why do we need the getter method to return the value of _phone property?

I also tried to remove the getter method and it seems that its still log the same things to the console.

1 Answer

Steven Parker
Steven Parker
212,246 Points

The getter allows the property to be accessed using the same name is it is set with ("phone" with no underscore).

But the getter isn't being used when you log the contents of the object, since it reports the property itself (with the underscore). But it exists for other code that might use the object.