Document Information

Last modified:
2007/05/31 17:39 by fjakobs

Understanding namespaces

qooxdoo organizes all JavaScript classes in namespaces just like Java does. Each class has a namespace which corresponds to its path name relative to the source/class directory and a class name which corresponds to its file name. In the qooxdoo SDK for example the button class is qx.ui.form.Button which means that it is in the directory qx/ui/form and has the filename Button.js. This is how the files are organized in the skeletons as well.

The browser loads all JavaScript into one flat namespace. Without namespaces it would be possible to have naming conflicts between functions or classes from different files. Since qooxdoo consists of nearly 300 JavaScript classes namespaces are a good method to organize the classes and avoid naming conflicts.

Namespaces become even more important if you use JavaScript libraries from different sources. Furtunately it has become good JavaScript practice to put as few variables as possible into the global namespace. qooxdoo defines just the variable qx in the global namespace. qx is the root namespace for all qooxdoo classes. Other big JavaScript libraries like dojo or Yahoo UI use namespaces to organize their code as well.

Definition from wikipedia:

A namespace is a context for identifiers. An identifier defined in a namespace is associated with that namespace. An identifier can be defined in multiple namespaces. The meaning of an identifier in one namespace is completely separate from the meaning it has in any other namespace. Thus, a namespace introduces a new domain in which one can define any identifier with the guarantee that it will not clash with existing identifiers (because those will be associated with other namespaces).

For example, Bill works for company X and his employee ID is 123. John works for company Y and his employee ID is also 123. The reason Bill and John can be identified by the same ID number is because they work for different companies. The different companies in this case would symbolize different namespaces. If the two men worked for the same company, and still had the same employee ID, there may be serious errors when it comes time to pay them.

This characteristic is the primary motivation for the use of namespaces. In large computer programs or documents it is not uncommon to have hundreds or thousands of identifiers. Without namespaces [...] it is hard to ensure uniqueness of identifiers in these cases. Namespaces provide an easy means to group logically related identifiers into corresponding namespaces, thereby making the system more modular.

In programming languages, namespaces are scopes that use the enclosing nature of the scope to group logically related identifiers under a single identifier. Many modern computer languages provide support for namespaces. In some programming languages (eg. C++, Python), these identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace tree. At the root of this tree is the unnamed global namespace.

Information

Last modified:
2007/05/31 17:39 by fjakobs

Account

Not logged in

 
 

Job Offers

To further improve qooxdoo we are seeking javascript developers. Read more...

Rich Ajax Platform (RAP)

RAP uses qooxdoo, Java and the Eclipse development model to build rich web applications. Read more...

qooxdoo Web Toolkit (QWT)

Similar to GWT this framework allows to create impressive qooxdoo applications just using Java. Read more...

Pustefix

Pustefix is a MVC-based web application framework using Java and XML/XSLT. Read more...

 
SourceForge.net Logo

Bad Behavior has blocked 0 potential spam attempts in the last 7 days.