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 trialSam Bailey
8,425 PointsWhy does this.owner = owner not work?
I'm having trouble understanding why we have to use the "backing property" of _owner, in place of owner. The constructor allows us to set, for example, this.animal = animal, so why can't we use the same syntax within the setter?
When I try it in the workspace, it gets a "Maximum call stack size exceeded" error, but I'm not sure why.
On an unrelated note, I find it a but odd that there are no other questions on this video... did they all get deleted or something? This topic is not exactly straightforward...
4 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsThe purpose of the backing property is explained in the video. :-)
Backing properties go with setter()
methods in OOP JavaScript. You need property and value that is equal to the single parameter that is passed to the method. That's how setter methods work. But you can't use exact names in the method, which is why the convention is to add an underscore to the name of the value.
The name of the property can never be the same as the name of a getter or setter method
I'm not exactly sure why this is. It's similar I think as to why you can't use variable names that are the same as JavaScript keywords. But the backing property is the thing that solves the issue.
Jesse Evans
Full Stack JavaScript Techdegree Student 8,967 PointsHI Sam -
I'm not confident I have the answer as I'm pretty green but here's where I am at trying to understand myself:
- maximum call stack size exceeded typically happens when some recursion won't resolve. e.g.: function knockKnock(){ console.log('who is there?'); console.log('Banana'); console.log('Banana who?'); knockKnock();}
- from looking at the setter code:
set owner(owner) {
this.owner = owner;}
I think what happens here is when we try to assign a value to owner, with "ernie.owner = 'Ashley';", the setter takes the argument owner, which then would be the right side of "this.owner = owner", but this.owner is taken as a call to the setter method which is then repeating the same thing again, never using the argument owner, before this.owner calls the setter again. - So i think change owner to _owner is a way to 'have orange knock on the door'/prevent unreconcilable recursion, since specifically, this.owner isn't a container that just needs a value, it's the setter method being called [the same way ernie.owner calls the method]. "this" is equivalent to "ernie" but just inside the object/class.
That's where I ended up pondering your question, I hope that helps, or that someone else can chime in and correct me if I am wrong.
Justin Kao
5,724 PointsWell.. That error is also called Stack Overflow
Because when you return this.owner
you're not calling the property
You're calling the METHOD set owner()
function
and guess what, that's an infinite recursion
so when computer memory gets full, you get the error Maximum call stack size exceeded
BTW, use markdown to mark your question, otherwise it's really hard to have a quick browse LOL
Samuel Kleos
Front End Web Development Techdegree Student 13,728 PointsChatGPT answered this for me –
Alright, let me break this down for you like you’re 12!
The setter method set owner(owner) is supposed to set a value for the owner property of the Pet class. But here’s the problem:
Inside the setter, you’re trying to use this.owner = owner. This creates an infinite loop, like a hamster running on a wheel forever. Why? Because:
- When you write this.owner, the setter method is triggered again (that’s what setters do when you assign something to the property they’re defined for).
- Inside the setter, this.owner = owner triggers the setter again. And again. And again. Forever.
- The program eventually crashes because it keeps calling the setter and runs out of memory, causing the “Maximum call stack size exceeded” error.
It’s like if someone asks you your name, and instead of answering, you just keep asking yourself, “What’s my name?” endlessly. You’d never actually answer!