public
Description: JSAN and Jause webapp
Home | Edit | New

Global Namespaces

Introduction
In JavaScript we all share the same global namespace (usually window). As your scripts get bigger and bigger and as you combine more and more libraries together conflicts are a real issue. An example is the popular libraries, MooTools and Prototype which will not run well together.

Establishing A New Namespace
First you should probably use the JSAN search to check if someone else is already using your namespace so that you do not create any conflicts. If you want to create a new global namespace and you want the community to use it then you should probably ask for input on the mailing lists .

Naming Your Namespace
Most global namespaces start with a capitalized letter A-Z, as they do in Perl, that way when you can code as such:

var Point = function(x,y) {
    this.x = x;    
    this.y = y; 
};
var clonePoint = function(point) { 
    return new Point(point.x,point.y); 
};

In this case you can feel confident the argument point won’t create any conflicts because it’s first character is lower case, as such the above code will run. However, if the point namespace wasn’t capitalized you might eventually mistake the point class and a point instance in your code.

dojo, dijit, dojox, qooxdoo

The above are cases of very popular JavaScript libraries that use Java-like namespaces, these libraries are acceptable in JSAN, however, they will be somewhat unusual because most global namespaces capitalize the first letter, as such:

YAHOO, YUI, Ext, MochiKit, PlotKit, AJS, Raphael, AJS, DOMAssistant, Sizzle, Sly, Peppy, QEvent, QFocuser, Titanium, CKEDITOR, Joose, Array, Object, Function, Date, String

All of those libraries fit into the global namespace, and they have their first letter capitalized, it is the same thing with most libraries on JSAN. Note that this should not discourage you from contributing your library if you have a different namespacing style, it just might not be as familar to JavaScript programmers, and users might accidentally override the library at some time.

Established Namespaces
Many namespaces such as Test have been established based upon their Perl counterparts.

DOM Interfaces with the current Document.
Task Deals with groups of concatenated files
Test The basis of all Testing modules
Last edited by jhuni, Thu Oct 08 16:51:15 -0700 2009
Home | Edit | New
Versions: