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

Quinton Shuman
Quinton Shuman
7,068 Points

So I've watched the video like 8 times and I just don't see what the value "owner" is representing in set owner(OWNER) {

So this is probably a dumb question and people probably won't be seeing this later on having the same question but hey if it wasn't a dumb question I could easily find the answer through a search engine and wouldn't have to ask it here. I just don't see what this parameter is taking in? itself? In other other examples on youtube and on duckduckgo and google people just pass in the word value and it just seems more and more like this is some kind of permanent placeholder. I've tried to get my little brain wrapped around this but it's been like a day and a half and at this point it is literally holding me back from progressing as I don't want to just know what word I'm supposed to put in the parameter, I want to understand what to put in the parameter. Please help. Thanx in advance. LYSM

I watched it 20 times, slowed it down another 10 times, and at the end, I thought I am not really a programmer-type-of-person! all I see is owner owner owner owner owner...

4 Answers

Hi Quinton!

In my book, there are no dumb questions.

Or to put it another way, the only truly dumb question is one that is never asked.

Just because something may be obvious to one person, it may not be to another (no shame it that).

Sometimes it's just difficult to see the forest for the trees, as they say!?!

OK, enough philosophizing!?! LOL

First, the variable name of the parameter being passed to virtually any function is arbitrary (meaning it can be anything you want to name it).

So, therefore:

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

Would work just as well if written this way:

set owner(myOwnerName) {
  this._owner = myOwnerName;
}

Or even:

set owner(x) {
  this._owner = x;
}

this._owner is the best naming convention in this case for that part of the code, though, because the setter is setting the "owner" property, so it makes the most sense (as does passing in "owner" as the parameter name instead of "myOwnerName" or "x".

Does that make sense?

Keep in mind that setters and getters are cool because you can do more in those functions than just set the value, such as some sort of transformation (like concatenation or making it upper or lowercase) or validation of the parameter value(s).

More info:

https://www.w3schools.com/js/js_object_accessors.asp

https://www.jackfranklin.co.uk/blog/es5-getters-setters/

I hope that helps.

Stay safe and happy coding!

Jason Rich
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Rich
Front End Web Development Techdegree Student 10,048 Points

Hey Quinton, I'm just coming across this lesson segment myself. Here's how I'm understanding it.

As Peter pointed out, the parameter could be named anything you like but convention dictates it use the same name as the property.

I just don't see what this parameter is taking in? itself?

    set owner(owner) {
        this._owner = owner;
        console.log(`setter called: ${owner}`);
    }

So, set owner(owner) is saying the Pet class property called .owner takes as its parameter the value of owner. Whatever value is passed as an argument owner will be set as Pet.owner, which is to say as the value of the Pet object's owner property.

I don't want to just know what word I'm supposed to put in the parameter, I want to understand what to put in the parameter.

It's using the same word "owner" because the owner of the Pet object is both:

  • the value of the Pet object's owner property or Pet.owner

  • the value of the argument passed into the set owner(owner) method

and should be one and the same.

So, set owner(owner) is basically saying "set the Pet object's owner property to the value of the argument passed in to the parameter (owner)".

If that makes sense.

Make sure you use back-ticks `, instead of single quotes ' in your console.log().

I unfortunately didn't immediately catch this, and only upon closer examination did I realize my error. Otherwise, you'll log "setter called: owner" vs. "setter called: Ashley" - this wasn't explained in the video and I'm not exactly sure why.

The (owner) in set owner(owner) is the parameter where we will put in the dog owners name. for example, if you write ernie.owner = 'Quinton'; , then you are basically putting set owner('Quinton'), and then the code will execute that in the setter and the output will be 'setter called: Quinton'.