This post demonstrates a simple namespace implementation. Two object prototypes named Person are created in two separate namespaces and thereby avoid convolution.
//if namespace exist use it, otherwise use a new object
var ENG = ENG || {};
//add person to the namespace
ENG.Person = function(){};
ENG.Person.prototype.sayHi = function(){
console.log("Good Day, My name is John");
}
//if namespace exists use it, otherwise use a new object
var GRM = GRM || {};
//add person to the namespace
GRM.Person = function(){};
GRM.Person.prototype.sayHi = function(){
console.log("Guten Tag, Ich Heiße Johan");
}
//create two people from two different namespaces.
var englishman = new ENG.Person();
var german = new GRM.Person();
englishman.sayHi(); // Good Day, My name is John
german.sayHi(); // Guten Tag, Ich Heiße Johan
Namespace switching isn't built into JavaScript, so it would be advisable to keep the namespace identifiers short, because they will have to be repeated.
Prominent libraries that encapsulate using namespace like structures:
$ - jQuery, Prototype
THREE - Three.js
For a library that offers a rich set of namespace functionality, investigate Namespace.js.