The method I’m advocating is based on an article on the Enterprise jQuery site. The whole approach is pretty bulletproof (someone else can’t go and re-declare undefined or $ and break your whole library). It allows you to declare private and public functions and extend your namespace later on – this ticks all the right boxes on my checklist.
Let’s have a look at an example.
This is very similar to the example in the link above – I would really recommend reading through it – it’s a much better explanation than I could come up with.
What I really like about this way of using namespaces is how easy it is to extend the namespace – you use exactly the same syntax.
In the code block where we are adding functionality we have access to all public methods and properties, but not to private members – which is pretty much what you would expect.