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

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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
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.