In most programming languages relatively mature and powerful, there is what we call a namespace. The namespace is a container name where we hold classes, functions and variables that can be repeated with the same name in other namespaces without involving an error. A example of what the real utility of organizing or namespaces is in the graphics libraries of programming languages, since in these libraries a very common element the Window class is usually referred to a window but may be in GTK, QT,. Net, Windows API, etc. (This is not quite correct since GTK would GtkWindow but for the example). Javascript as a language and more to mature over the years and relatively standardized by modern browsers, despite being a multiparadigm programming language, as do PHP, also has namespaces, but due to weakness of these namespaces variables are more virtual than real.
And understanding that it is serving a namespace to declare it in javascript, what we should do is use arrays to generate. Thanks to the weakness and dynamism of the variables in javascript, we can assign to a variable array of positions by name and access these through a punctuation mark "." and not their common key array ["key"], so here comes the trick. Basically what we do by declaring a namespace in javascript is assigned to a variable array with the positions of the namespace and then and within these positions, log in as if from .Net or Java in question. With an example is simpler.
var Indalcasa = { Tools : {} }; Indalcasa.Tools.MyClass = function() { this.variable = "value"; this.myMethod = function(parameters) { return this.variable + " " + parameters; } }; var iClass = new Indalcasa.Tools.MyClass(); iClass.variable = "new value"; alert(iClass.myMethod("test"));
The above example shows an alert window with a message such that "new value test", as we have instantiated the namespace class, we changed the value to the class attribute and the class method, concatenate the value of class attribute to the parameters that we have passed. This is a small and simple example of what can be done with javascript, namespaces and classes.
Via: Namespaces o espacios de nombre virtuales en javascript