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 Setters

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Where exactly we use the owner parameter inside: set owner (owner)?

Hi guys,

For me the 1 and only parameter is confusing a little in this part, when we create the setter method:

 set owner (owner) {

Later when the user type the value of the given property (owner), she/he has to write:

ernie.owner = 'Peter';

I understand that we set the value of an object like this: objectname.dot = 'XY', but if we set like this, why we need the owner parameter in this code:

 set owner (owner) {

Because in other cases when we set a parameter inside the parenthesis:

class Pet {

  constructor (animal, age, breed, sound) {

    this.animal = animal;
    this.age = age;
    this.breed = breed;
    this.sound = sound;

  }

We used it later like this (gave the values of the Barney object properties) :

const Barney = new Pet ('bird', 2, 'nice bird');

1 Answer

Steven Parker
Steven Parker
216,133 Points

I understand your confusion. A setter is a bit different from a typical method in that the syntax for using it looks like an assignment instead of a method call. This is intentional to make it consistent with setting a value in a field, and a complement to the syntax of a getter which also mimics getting a value from a field.

As an alternative, you can always create a conventional method that performs the same job and call it instead.