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 Setters

Why 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...

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

The 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jesse Evans
Full Stack JavaScript Techdegree Student 8,967 Points

HI 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:

  1. 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();}
  2. 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.
  3. 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.

Well.. 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