On 11/16/06, Dragan Krstic <[EMAIL PROTECTED]> wrote:
And what is benefit od namespaces?
Namespaces prevent naming collisions between functions that have the same
name but do different things (or the same thing in different ways). For
example, I have a function called a debug which simply alerts whatever I
pass into it:
function debug(info) {
alert(info);
}
You also have a function called debug, but it is much more involved and has
a lot more features.
function debug(info) {
// do all sorts of fancy stuff
}
Both of our debug functions have their uses (mine is quick stuff, and yours
is for when you need more info and want it presented in a certain way), but
if we included both of them on the same page, then one of them would get
overwritten by the other (whichever one came first would get overwritten by
the one that came second). The solution to this problem is to use
namespaces:
var aaron = {};
aaron.debug = function(info) {
alert(info);
}
var dragan = {};
dragan.debug = function(info) {
// do all sorts of fancy stuff
}
Now I can use both of our functions in the same page without causing
problems by calling
aaron.debug("some debugging info");
and
dragan.debug("some debugging info");
function package(ns) {
ns = ns.split('.');
var cur = window, i;
while ( i = ns.shift() ) {
if ( !cur[i] ) cur[i] = {};
cur = cur[i];
}
}
This function simply automates the process of creating a namespace. So I
could do
package("really.long.namespace");
really.long.namespace.debug = function(info) {
// do stuff
}
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/