0 points - 5 years, 7 months ago by RobertTheGrey(490) under Other ()
0
You either love it or you hate it, but in the end, the JavaScript Module Pattern is more a force for good in most developers' eyes. We review a number of posts to point you in the right direction.

A JavaScript Module Pattern

Yahoo! User Interface BlogSo this is pretty much where it all started, at least in terms of a formal definition with a simple example. Eric Miraglia explains how and why this pattern is employed in the YUI library and how to namespace your own modules to avoid leaking into the global space causing, or being affected by unintentional side-effects or conflicts.

This post really does start at ground zero with an example that is very easy to follow and digest. The explanation is clear and detailed enough not to leave beginners in the dark. This would be a good place to start if you're just coming across this technique.


Why I Don't love JavaScript's module pattern

Why I Don't love JavaScript's module patternThis pattern is not without those that disagree with its use. Jonathan Snook talks about why this pattern causes him more friction during his development process than it's worth.

He talks about how the pattern hinders live debugging scenarios and object extension as well as bringing up some interesting view points on AOP and overall malleability as it pertains to JavaScript.

Personally, I can't help but disagree with this assessment given the way I code and find it helps rather than hinders my development process, but his points are worth noting to see if they would affect your work. Understanding some of the potential friction points may help your decision.


Better JavaScript – Namespaces

Better JavaScript – NamespacesIn this post, Jaco Pretorius gives us the short and sweet version as part of his "Better JavaScript" series, basically distilling the idea down to the bare minimum parts. The same article has been cross-posted right here on DevDirective as well with some minor tweaks.

If you're looking for something more in depth, this article is not for you, but if you're just trying to explain the concept to a junior who is just starting out, then this will be at just about the right level of complexity.


Basic JavaScript Part 10: The Module Pattern

Basic JavaScript Part 10: The Module Pattern In this post Jan Van Ryswyck explains why this pattern is useful for packaging scenarios and provides a nice example depicting a package for managing media of a podcasting site.

He goes through the down sides caused by unexpected replacement of functions, how to protect against that, as well as different methods of achieving a clean outcome for your package and the internal methods it relies on to function properly.


How Good C# Habits can Encourage Bad JavaScript Habits

How Good C# Habits can Encourage Bad JavaScript Habits Many developers approach JavaScript like they would many of the C-syntax based languages out there. The problem is that most of those are statically typed languages who's definitions around variable scoping differ vastly from that of JavaScript which is a dynamic language.

Elijah Manor sets us on the straight and narrow with this series of posts and in Part 1 covers the reasons that variables declared on the global scope in JavaScript are detrimental. This excellent post is definitely worth a mention here even though the posts cover more than just namespacing in JavaScript.

This posts leans more towards the experienced developer, but the explanations are so good, that I wouldn't hesitate throwing a junior in the deep end with this one so that they can learn to swim - goodness knows, this article provides some top quality water wings!


JavaScript Module Pattern: In-Depth

JavaScript Module Pattern: In-Depth For me, the canonical post on this topic comes from Ben Cherry at his blog: Adequately Good. This post pretty much covers everything you need to know about that pattern, including some advanced scenarios that cover Loose and Tight Augmentation, Cross-File Private State, Sub-Modules and Inheritance. A veritable feast of ideas and applications for the pattern.

He also goes into more details around proper unit testing and how to effectively debug code written this way, so be sure to check out his blog if this is your poison.


Conclusion

Hopefully that gives you enough to get on with when learning this pattern. Let me know in the comments if I've missed a particularly good one and I'll be sure to update the post.

Until next time,
RobertTheGrey