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 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
231,184 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