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

Joseph Overton
Joseph Overton
4,234 Points

Is backing property syntax not necessary in this case? this._x = x

In the Ashley's last course on getter and setter methods, when she was setting a property with the same name as the argument being passed in, she used a backing property that had an underscore in front of the name. The example i copied down was:

set owner(owner) { this._owner = owner;}

SO when i was writing this constructor method, I used that syntax, but she didn't. Any guidance on why this isn't necessary in this instance?

Joseph Overton
Joseph Overton
4,234 Points

I was assuming this question was going to be tied to the lesson i was doing, but, in case it isn't, this is where i am.

https://teamtreehouse.com/library/board-and-space-class-constructor-methods-solution

...specifically the constructor method for the Space class

1 Answer

Steven Parker
Steven Parker
231,141 Points

I didn't see any getter or setter used in that video.

But just in general, with a getter and setter in place, it would not matter if the property were referenced by name, or if the backing variable was accessed directly. A little more code would be involved in using the the property by name, but either way would cause the backing variable to be read or set.

Joseph Overton
Joseph Overton
4,234 Points

no getter or setter methods in there, but i had assumed that I'd need to do the same thing with the constructor method. I had initially coded it like this
constructor(x, y) { this._x = x this._y = y }

her solution version just has constructor(x, y) { this.x = x this.y = y }

Steven Parker
Steven Parker
231,141 Points

The result is the same, hers just uses the setter and yours goes directly to the backing variable.

Glad I could help. You can mark a question solved by choosing a "best answer".
And happy coding!