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 trialSamuel Cleophas
12,348 PointsWhy does the setter 'method' not take in arguments?
Why doesn't this code work? I feel like a lot of things are wrong.
class Owner {
constructor (name, address) {
this.address = address;
this.name = name;
}
set mobile(phone) {
this._mobile = phone.replace(/[^0-9]/g, '');
}
get mobile() {
return this._mobile;
}
}
const ernie = new Pet('dog', 1, 'pug', 'bark');
const owner = new Owner('Samuel', '18, Druitt Street');
owner.mobile.phone('(+61)447 21 4934');
console.log(ernie.owner.mobile);
After watching these videos, because the argument, property, and method are all named the same in the video, it just feels like I don't understand which one is which.
I named them differently to help myself understand what the heck is going on but I still don't get it. :(
3 Answers
Steven Parker
231,264 PointsSince you renamed things, "phone" is not a property now, but "mobile" is. So to set and then get that property:
owner.mobile = '(+61)447 21 4934';
console.log(owner.mobile);
I also removed the line creating the constant "ernie" since "Pet" isn't defined here.
And setters are defined like methods, but to use them you make assignments as if they were variables.
romaug
16,527 PointsTry
owner.mobile = '(+61)447 21 4934'; instead of owner.mobile.phone('(+61)447 21 4934');
console.log(owner.mobile);
Przemyslaw Chmiel
13,532 PointsTry this... const phoneNormalized = phone.replace(/[^0-9\.]+/g, '');