ajile: Learn: FAQ: Why Use Namespace?



Back to Documentation


Q2: Is the Namespace directive necessary to define a namespace'd script?

A2-General: Yes.

A2-Part 1: The Namespace directive affects the global environment. That is to say, when the Namespace directive is used, it creates a namespace within the global environment, the browser’s window. So for example:

Namespace ("com.iskitz.examples");
creates the com.iskitz.examples namespace in the global environment so that a script may be defined as follows:

com.iskitz.ajile.examples.Simple = function() { this.toString = function toString() { return "[Simple]"; }; };
Without the Namespace directive the browser would throw an Exception stating that com.iskitz.ajile.examples is undefined/null.

A2-Part 2: When multiple scripts that claim membership to the same namespace are loaded within the same environment, the first script loaded defines the common namespace. Subsequently loaded scripts within the same namespace will work without using the Namespace directive because the namespace would have already been defined in the global environment by the first loaded script.

This effect is evident in the Complex and Simple example scripts. Complex defines the namespace com.iskitz.ajile.examples in the global environment so commenting out the Namespace statement in the imported Simple script has no negative effect on the Simple script’s definition and operation.


Back to Documentation