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

Can not set property of Undefined.Why??

i dont know what wrong with this code. It giving the error cannot set the phone propert of undefined(Owner is object for which we need to set phone property). but when we check Owner object it is available

class Pet{
  constructor(animal,age,breed,sound){
  this.animal=animal;
  this.age=age;
  this.breed=breed;
  this.sound=sound;
  }

 get activity(){
const today=new Date();
const hour=today.getHours();
  if(hour>8 && hour<20){
    return 'Playing'
    }
  else{
    return 'Sleeping'}
}
 get onwer(){
   return this._owner;
 }
set owner(ownerName){
 this._owner=ownerName;
  console.log(`Setter Called: ${this._owner}`)
}

}
class Owner{
  constructor(name,address){
    this.name=name;
    this.address=address;
  }
  set phone(phone){
  const phoneNormalized=phone.replace(/[^0-9]/g,'');
  this._phone=phoneNormalized;
  }
  get phone(){
      return this._phone
  }
}

  const ernie=new Pet('dog',8,'sheffered');
 ernie.owner=new Owner("Muhammad Ali","Ranjha");
 ernie.owner.phone='(555) 456-9788';
console.log(ernie.onwer);

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Muhammad Ali! You just have a couple of typos here and there. For instance, around line 18, you typed get onwer(), but you meant to type get owner(). Note that the "n" and "w" are backward in your version. In the console.log() on the very last line you have the same type of misspelling. You typed ernie.onwer but you meant to type ernie.owner.

Hope this helps! :sparkles: