This general practice is documented at http://www.prototypejs.org/api/array, where it is also explained how caching the length property is an aid to performance.
(Though for the stated purpose of creating namespaces, which will probably only be handling single-digit lengths for the majority of the cases, the savings will be practically zero.)

Look at the part within the for loop. If the object exists, it is used, otherwise a new object ({}) is used.

I just looked at your link, and it does this part exactly the same way. However, it is also hard-coded to check against a "DTS" parent, which I'm not at all a big fan of - as noted in the link to the YUI bug #1944624.

It's really cool of you to post this namespacing technique. However, as an intermediate Java programmer who is pretty new to JavaScript, I'm pretty confused about how to make use of the namespace function to ensure that my classes all stay within my defined namespace. Is there any way you could post a zip file or something of a mini-quickstart app that just shows 3 js files that do simple stuff and take advantage of your namespacing technique?

Mark, in javascript, any variable that is not explicitly declared with the "var" keyword will become global, wherever you defined it. This is one of javascript's most unfortunate big warts. Try this sequence of inputs in the Firebug console if you want to convince yourself -- the latter "len" will be 12 in the global environment:

Anonymous: Not necessarily implemented once per file, but once per scope. On the web and in XUL, this is pretty much once per window. I would always declare it using "var". Whether it becomes global or not depends upon the scope in which it is defined. For now, I would declare this within a closure / own function block, and then create the namespaces while you still have a valid reference to the defined namespace function.

Unfortunately, I don't see a better solution for this, short of such a namespace function or other equivalent functionality being accepted as a language standard.

Calling a namespaced function from XUL isn't any different than the example given above in the post.

Antonio - I completely support your effort, as I insist that all my code is warning-free as well. That said, I'm not able to reproduce the issue that you're observing. Are you seeing it in the initial evaluation of the function, or in an execution of the function? If an execution, can you please provide an example call that demonstrates the warning you observed? Thanks!

Notes

Like something you see?
Please feel free to make a comment,or link to here from your site!

Please sign-in (using a Blogger, Google, or OpenID login) to post any comments; otherwise, please at least post your name.
If you don't want your name behind a comment, it probably doesn't belong on my blog.

While I appreciate comments, I reserve the right to remove any comment for any reason, including inappropriate content, spam, or other additions with zero-value.
(Even constructive criticism has value!)
All comments are now moderated due to spam and other abuse.