I'm trying to up my game, learning and want to adopt a best practice with regards to Namespaces to separate my code from potential overlaps of function names, variables etc.
I've come across several mechanisms for implementing this but one thing puzzles me.
Many approaches initialize the global Namespace variable like this:
var ns1 = ns1 || {};
So this says "if ns1 exists, use it, else create a new variable". But if within that variable, I then create a second function with a duplicate name, it will replace the original. I have a JSFiddle here. The thing that's confusing me is: why use an "or" in the global variable assignation? Since we are trying to separate things out, this doesn't make sense to me. Am I missing something fundamental here ?
Or is it as simple as the possibility that I may have several libraries, so I am re-using my own variable. In which case I should just ensure that "ns1" is actually something much more unique so the likelihood of clashes is reduced? Sorry if this seems a dumb question but I'm just worried I'm missing something and want to do this right. Maybe others in my situation are also confused and this may help. Thanks.