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

4 Answers

Steven Parker
Steven Parker
231,269 Points

You would normally invoke "set" first, or else there would not be anything to "get".

But if you're talking about the order they are defined in the class, that makes no difference.

Ian Ostrom
seal-mask
.a{fill-rule:evenodd;}techdegree
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,332 Points

In JavaScript the order of function definitions doesn't matter because of function hoisting. Order matters in other languages.

Steven Parker
Steven Parker
231,269 Points

Methods aren't hoisted, so don't try to call them before the class is defined.

Ian Ostrom
seal-mask
.a{fill-rule:evenodd;}techdegree
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,332 Points

Steven Parker

Thanks for the correction on methods inside classes. Though, in trying to confirm what you've said I've had trouble finding a clear answer for why the order of class methods doesn't matter.

According to MDN's documentation classes are not hoisted. However, I've also read that classes are hoisted, but just not initialized until evaluated thus the reason for not calling a class' functions until after the object is instantiated.

This post also says that hoisting of functions happens within block scope. So am I correct in concluding this is the reason that the order of functions within a class do not matter? Or is it only that we could never have a function inside a class that can directly call another function in that class? I couldn't come up with any.

Steven Parker
Steven Parker
231,269 Points

Sure, methods can call each other. But since they are all defined together in the class, and all instantiated together, they are all available before you call any of them.