One of the first things you learn programming in JavaScript is, "Global variables are evil." This is especially true when writing a JavaScript library, but I'd take that advise even otherwise to avoid unnecessary headaches.
As an avid jQuery user, one thing I do in almost every project is define a namespace function on the jQuery object.
The second example shows a case where an function is passed instead of an object. This is useful for many reasons which I won't get into... actually you can read a great post here about JavaScript module patterns.
And when jQuery isn't being used on a project, the following does the exact same.
function namespace(ns, obj) { var ns = ns.split('.'), p = window, i, k; for(i=0; iblog comments powered by Disqus