Richard said:
A simpler way of doing that is:-
var com = {unfocus:{}};
That's true, but if someone else already has properties on the com
object, this would overwrite their simulated namespace (like com.deconcept).
// do first one (doesn't need eval)
if (!window[namespace[0]]) window[namespace[0]] = {};
Stuffing references to objects into the global namespace
using arbitrary names does not seem like a good idea at
all. The potential for accidental naming conflicts seems
considerable.
Well this actually keeps all of the namespace objects
contained under the base namespace (I use com.unFocus
in all of mine for example).
If you always use the same single object in the global namespace there
is no need to start the assignment process with - window -, but if you
do you are allowing the creation of named properties of the global
object with arbitrary names.
So the only object in the global namespace is com.
Only if there is no possibility of this code being employed by scripts
with a different namespace.
That's true, but it seemed (to me at least) that languages that have
formal namespace support would have this same type of problem.
If you write the code that stuffs the names into the global namespace it
is your responsibility to handle the issues of doing so.
That's worth considering.
You are only making javascript look like other languages (and then not
that much like them). Javascript is going to behave like javascript, and
all this namespace qualification is just going to bloat the code and
impose a runtime efficiency cost. Which the use of namespaces in
compiled languages the cost of resolving those namespaces will tend to
happen when the source is compiled rather than each and every time any
particular statement is executed.
Getting those term definitions right is important. ;-) You are right, it
does just look like other namespace implementations (sort of) which is
actually what I really meant by behave.
The structures in javascript that are most analogous with classes in
class-based languages need only impose (at most) one name on the global
namespace per 'class'.
In a class-based language there is a good chance that a library of
hundreds of packages already exists, alongside any created by the
developers using the language. Thus a class called, for example,
"Document" is likely to occur in a number of packages, and need to be
more qualified in order to distinguish between them. In javascript there
are no libraries/packages distributed with the language, and the
'classes' used in any particular browser scripting context are further
restricted by the need to download the source code, so it is actually a
bad idea to be including anything that is not actually needed in that
context. In any specific context (even a fairly large web application)
it is both unlikely that the number of 'classes' being used would grow
to an unmanageable level (at least if the author understood what they
were doing) and unlikely that two classes would even suggest using the
same name.
In what sense "distribute"?
I'm working on a set of scripts that will (hopefully) make it easier to
allow flash (or AJAX) developers to add browser back button support, and
deep links to their projects. I have a bunch of classes, including some
utility classes that I use to make this work. I started without using
this style of namespace, and found that I was coming up with long names
to prevent my Classes from clashing with other projects (names like
unFocusFlashCommunicator). Then I found information about that namespace
technique, and figured that would be a good way to keep my scripts from
clashing with an implementor's script names, without using excessively
long class names (so I'd use com.unFocus.Flash.Communicator). I have
realized though that the names are still long, but it seems better
organization, and it is pretty easy to "import" the classes to the
global space.
I have thought that the script tag is kind of analogous to an import
statement in namespace supporting languages - meaning, only scripts that
have been linked into the current page are actually in the global space
(I think this is what you were getting at). So thinking about it in that
way, organizing them might make more sense, if I just organize my
classes in a namespace style folder hierarchy, then just link them into
the document that needs to call on them.
Maybe that would be a better way to organize classes to take advantage
of javascript's unique strengths, instead of trying to make it look like
other languages, though in some cases, I may still create collections of
useful methods (kind of like static classes I guess).
Thanks Richard.
Kevin N.