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 trialJesse Dispoto
Front End Web Development Techdegree Graduate 14,538 PointsDoes it matter what order you invoke the getter and setter methods? Does get always go before set?
Does set always follow get, or could you put set before get?
4 Answers
Steven Parker
231,275 PointsYou 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
Full Stack JavaScript Techdegree Student 10,332 PointsIn JavaScript the order of function definitions doesn't matter because of function hoisting. Order matters in other languages.
Steven Parker
231,275 PointsMethods aren't hoisted, so don't try to call them before the class is defined.
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,332 PointsThanks 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
231,275 PointsSure, 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.
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,332 PointsThanks Steven. That makes sense.
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,538 PointsJesse Dispoto
Front End Web Development Techdegree Graduate 14,538 PointsYeah, I meant the order they are defined in the class. Thank you!
Steven Parker
231,275 PointsSteven Parker
231,275 PointsJesse Dispoto — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!