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

Tsipporah Christopher
Tsipporah Christopher
Full Stack JavaScript Techdegree Student 6,284 Points

phone.replace() doesn't work, and when I try to return the phone it comes back undefined

Hello I am having trouble getting the replace property to work. And later when I try to directly call the phone property it returns undefined. Thank you!

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 location() {
    const today = new Date(); 
    const day = today.getDay(); 

    if (day == 0) {
      return 'outside';
    } else {
      return 'in the backyard';


    get owner() { 
    return this._owner;   

    set owner(owner) { 
      this._owner = owner; 

    speak() {


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

    set phone(phone) {
        const phoneNumber = phone.replace(/[^\d]/g, '');
        this._phone = phoneNumber;

    get phone() {
        return this._phone;


const golden = new Pet('dog', 1, 'pug', 'bow wow');
const charm = new Pet('dog', 8, 'border collie' , 'woof woof');

golden.owner = new Owner('Isis', '123 main street');

golden.owner.phoneNumber = '(123) 456-7890';




1 Answer

Steven Parker
Steven Parker
217,571 Points

The phone.replace() code looks good, but it isn't being used here. The setter code only runs when you assign something to the phone property, but the later code is creating a new property named "phoneNumber" instead.

That's also why the getter for phone returns "undefined", as the backing value has never been created.