Yes, I think you are right really. There will always be some impact
on the global namespace that could cause problems and limiting it is
best done by using closure as outlined in the article you posted. As
you may notice from my blog thanks to you I've recently discovered
those techniques by reading your FAQ article on jibbering.com which
was nothing short of revolutional to my JavaScript coding (Thanks!).
My idea is that by having a package style system you are imposing a
convention as well as limiting impact on the global namespace. What
I've developed is a simple namespace() function. This basically takes
a package name then builds the correct objects if necessary (but only
if necessary) so
namespace("net.danwebb.util").isValid = function() {
....
}
would evaluate to net.danwebb.util but then if you declared a function
to a different namespace like:
namespace("net.danwebb.event").anotherFunction = function() {
}
then this would only build the objects that weren't created. With
this convention in place scripts would all fit within the heirarchy
and should keep the namespace quite clean.
I too don't agree with monolithic generic scripts but over the years
I've built up tons of random scripts and often need to apply them in
various combinations on the same pages and this is where the idea was
born. Also, before Flash MX 2004 I saw alot of utility in producing
large general purpose libraries (as filesize isn't an issue) and alot
of problems with namespacing when working on large projects.
An example script packaged in this way is something like this:
http://www.danwebb.net/tests/slideshow/event.js
which is a little set of event handling things I wrote which fits in
with several other scripts into this:
http://www.danwebb.net/tests/slideshow/
(Which is very much work in progress and pretty dodgy at the moment so
don't tear it apart just yet :) ) But you should get what Im aiming
at.
Do you think there's any milage in this at all? Im half quite into
the idea but also have alot of the same doubts as you outline in your
reply. Ive been thinking about it a bit too much and have lost a bit
of perspective. Perhaps all the Java I've been doing recently has
left me trying to Javatize my JavaScript (Which is definately almost
always a wrong 'un).
Thank for your feedback though Richard, much appreciated.