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 Object Interaction

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

Cannot set property number of #<Owner> which has only a getter

I am getting the error, when trying to run the code:

Cannot set property number of #<Owner> which has only a getter

class Pet {

  constructor(animal) {
  this.animal = animal;

  }


get activity() {

  const today = new Date();
  const hour = today.getHours();

  if(hour < 8) {

    return "sleep";

  }
  else {

    return "woke";

  }

}

get owner() {

  return this._owner;

}


set owner(owner) {

  this._ownerName = owner;
  console.log(owner + "setter");


}
}


class Owner {



  constructor(name, number, gender) {

    this.name = name;
    this.number = number;
    this.gender = gender;


  }

  set(number) {


  const numberformatted = number.replace("abc", "");
  this._number = numberformatted;



}

  get number() {

    return this._number;



  }






}

const animal1 = new Pet();
animal1.animal = "dog";


owner1 = new Owner("Ashley", "abc12345", "female");
console.log(animal1.owner1);

1 Answer

Steven Parker
Steven Parker
218,654 Points

It looks like you're varying a bit from the video, but it looks like:

  set(number) {         // instead of this...
  set number(number) {  // you probably meant THIS