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

Maxim Melnic
Maxim Melnic
4,178 Points

We can add return this.owner in set method?

Hi, I tried to add return this.owner in set method and got the same result as with get return method.

How so?

5 Answers

Emmanuel C
Emmanuel C
10,634 Points

It may not thrown any errors if you put a return in the set, but when you call the property it looks for the get method and comes back as undefined since there is no get method. That line errors, because youre calling owners get method, then calling phones set method. If you console.log(ernie.owner = "Asley"); itll show asley in the console, since youre calling the set method and it has a return in it. But after you set, then console.log(owner); its calling the get method and since none exist, its undefined.

I hope that makes sense, and that I understood your question correctly.

Emmanuel C
Emmanuel C
10,634 Points

You're not supposed to return anything in the setter, that's the getters job. Instead you pass in a value to set the property to.

To answer your question, a function will do what you code it to do, it doesnt matter what you name it. So if you name it setOwner, but make it return a value, it will return that value.

Maxim Melnic
Maxim Melnic
4,178 Points
class Pet {
  constructor (animal, age, breed, sound) {
    this.animal = animal;
    this.age = age;
    this.breed = breed;
    this.sound = sound;
  }
  speak () {
    console.log(this.sound);
  }
  get activity () { 
  const today = new Date();
  const hours = today.getHours();

  if (hours > 8 && hours <= 20 ) {
    return 'playing'; 
  } else {
    return 'sleeping';
  }
}

set owner(owner) {
  this._owner = owner;
  console.log(`setter called: ${owner}`);
  return this._owner; //IT WORKS without GET!!!!!!!!!!
  }
}

class Owner {
  constructor (name, adress) {
    this.name = name;
    this.adress = adress;
  }

  set phone (phone) {
    const phoneNomilized = phone.replace(/[^0-9]/g, ''); 
    this._phone = phoneNomilized;
    return this._phone;
  };
};

const ernie = new Pet("dog", 1, "pug", "yip yip");
const vera = new Pet("dog", 8, "border collie", "woof woof");

ernie.speak();
vera.speak();

//ernie.owner = "Asley";
ernie.owner = new Owner("Missa", "123 Marin Street");
//ernie.owner.phone = '23423-4324(32)'; //if we uncomment this line, we get an error without get method!!!!!!
console.log(ernie);
Maxim Melnic
Maxim Melnic
4,178 Points

Setter was return value and was able to save new property, but when I tried to interact with another object(class) I started getting an error.

Emmanuel C
Emmanuel C
10,634 Points

Can you post your code?

Yeah was looking for this question, thanks both.