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

Why complicate OOJS?

In the video... ('https://teamtreehouse.com/library/setters') towards the end of the video, Ashley wants to log the value of

owner

to the console. However, I don't understand why she would make a whole get method to call upon the already created _owner variable instead of simply calling

console.log(ernie._owner)

This would be a much simpler way no? As I'm programming, if I know this is a setter method I just remember to call the variable with an underscore like the preferred syntax in the actual method is suggested. I mean, is it necessary to make a get method? If so, why?

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

You are correct in this case, there is less complexity and more efficiency to directly modify this variable.

But the intent is to introduce you to common design patterns, whole books are written on this topic.

Interviews for developer jobs often ask applicants about common design patterns in JavaScript! And they're expecting you to demonstrate a familiarity with one or more of the following design patterns-- "Decorator," "Mixin," "Factory," "Facade," "Mediator," "Module," etc.

Don't worry, you will learn plenty about this as you work through Treehouse's JavaScript tracks.

The underscore on the name "_owner" is a tip-off (or specific intent) that it is meant to be a private variable to the class instance (typical of a Module pattern). The underscore means "stay away" or "be careful" of modifying/reading because it could have unexpected results.

Feel free to SKIM the resources below for a hint at what is coming up in JavaScript--

https://addyosmani.com/resources/essentialjsdesignpatterns/book

https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know

Thank you so much for the explanation and the resources!

Awesome answer thanks !

Hamdi Elshahat
Hamdi Elshahat
2,565 Points

That was an amazing answer, could you please tell me which courses you talked about because I always believe that programming isn't just syntax. and I want to learn more about these styuff.

David Douglas
David Douglas
3,719 Points

Is there a common practice for a creating a setter for an existing property. Is the best practice to provide an underscore to the existing property. If, so would it still be common practice to create a getter (get owner(){}) to retrieve the value or could you then just call the property as seen below

class Car{ constructor(make,owner){ this.make=make; this._owner = owner etc... }

set owner(newOwner){ this._owner = newOwner } }

const car1 = new Car('Toyota','James') car1.owner = 'Donald'

console.log(car1._owner) //Prints to the console 'Donald'

Hamdi Elshahat
Hamdi Elshahat
2,565 Points

Be careful in getter you can make changes on _owner like logical operations or something so when you use _owner get a wrong value. I think this what was referred by @Jeff Muday answer's in "he underscore means "stay away" or "be careful" of modifying/reading because it could have unexpected results."