Notes:
- Namespacing is a good practice because you have less chance of having conflicting names with other scripts. This way, only your name-space has to be unique, but multiple name-spaces can have the same functions in them.
- jQuery DOES use namespacing, and you do not need the plug-in. The jQuery object itself is a name-space. . . Any function inside jQuery is 'name-spaced' in the jQuery object. This is true for any JavaScript object.
In response to Amir's comment:
YUI achieves namespacing by adding a variable to the YUI object where the added variable is also an object. Their namespace function just builds it for you.
var lib = {
util:{},
widget:{},
tool:{}
};
//creates a function named reset in the lib.util namespace
lib.util.reset = function() {};
//creates a function named reset in the lib.widget namespace
lib.widget.reset = function() {};
In jQuery, you add it to the jQuery.fn
(or $.fn
if you use $
for jQuery() namespace.