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

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
231,248 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.