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

RangeError: Maximum call stack size exceeded

Snapshot: https://w.trhou.se/nqjslftqtk I tried following along the video, but can't get the same output as the instructor. I get the following output: setter called: [object Object]
/home/treehouse/workspace/Pet.js:39
const phoneNormalized = phone.replace(/[^0-9]/g, ''); ^

RangeError: Maximum call stack size exceeded
at String.replace (<anonymous>)
at Owner.set phone as phone
at Owner.set phone as phone
at Owner.set phone as phone
at Owner.set phone as phone
at Owner.set phone as phone
at Owner.set phone as phone

Apparently the class member needs a different name then the ones used within the setter and getters, otherwise they keep calling themselves and you get the stack size exceeded (stack overflow). I made these changes to your class and it works now:

class Owner {
    constructor(name, address) {
        this._name = name;
        this._address = address;
        this._phone;
    }   

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

    get phone() {
        return this._phone;
    }
}

1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

I had a similar result. I double checked the video, and in it, Ashley adds underscores just before the phone property in the getter and setter, like so: this._phone. Adding the underscore to those two spots fixed it and the code ran fine.