The JavaScript Module Pattern

Eric Miraglia has just posted an excellent tutorial on the YUI Blog demonstrating how to use Douglas Crockford's awesome Module Pattern for JavaScript.

I learned about this pattern when I randomly stumbled across a page about it on Yahoo!'s internal wiki shortly after joining the company, and I immediately fell in love with it. I was amazed that I couldn't find any mention of it outside Yahoo! aside from a few hints on Doug Crockford's website, so I'd been meaning to write a tutorial, but never got around to it.

Luckily, Eric did the hard work for me, so now all I have to do is link to his article. Hooray!

One thing Eric's article doesn't mention, though, is how to extend a base module in such a way that the inheriting module still has access to members of the base module's private scope. It's a little tricky, and there are caveats, but luckily Douglas Crockford has gifted the world with the solution to that problem too. I'll try to find the time to write about it soon (if the YUI blog doesn't beat me to it).