Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Getters and Setters Setters

4 Answers

Steven Parker
Steven Parker
216,120 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,306 Points

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

Steven Parker
Steven Parker
216,120 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,306 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
216,120 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.