namespace.lookup(<string>) - is designed to return the same object - even if the code in the namespace has not yet been loaded (defined). For orthoginality of design, the lookup function is separated from the define function (define could have been designed to do a lookup and THEN a define operation).
You can see a common usage pattern here: http://code.google.com/p/pageforest/source/browse/appengine/static/src/js/client.js This 'client' namespace initializes references to several other namespaces when it first loads: namespace.lookup('com.pageforest.client').defineOnce(function (ns) { var storage = namespace.lookup('com.pageforest.storage'); var cookies = namespace.lookup('org.startpad.cookies'); var base = namespace.lookup('org.startpad.base'); var format = namespace.lookup('org.startpad.format'); var dom = namespace.lookup('org.startpad.dom'); var dialog = namespace.lookup('org.startpad.dialog'); var vector = namespace.lookup('org.startpad.vector'); var random = namespace.lookup('org.startpad.random'); ... On Jan 19, 7:56 pm, "Nicholas C. Zakas" <[email protected]> wrote: > ?This is an interesting approach, kind of a mix between the YUI 2 > namespacing scheme and the YUI 3 sandboxing scheme. I'm assuming that your > design goals are: > > 1. Create only one new global (namespace). > 2. Do not create any additional properties on namespace (hence define() and > lookup()). > > I think you achieve these goals elegantly. As a matter of preference, the > define() and lookup() methods seem like an extra layer of abstraction that > may not be necessary. For instance, you could pass an object into the > function that already contains the previously-loaded modules and allow > others to attach onto that (see: CommonJS export). > > I'm interested in what happens after you've define all of your modules, as > in, how do I begin using the code I've defined? > > -Nicholas > > _____________________________________________________ > Nicholas C. Zakas > Twitter: @slicknet > Blog:http://www.nczonline.net/ > > > > > > > > -----Original Message----- > From: Bal zs Galambosi > Sent: Wednesday, January 19, 2011 3:59 PM > To: [email protected] > Subject: Re: [JSMentors] Feedback on namespace pattern implementation > > 2011/1/19 mckoss <[email protected]>: > > I've been using a namespace pattern I developed and would love to get > > feedback on my implementation/design. I know there is some wheel re- > > invention here - I just have not yet found other patterns I like as > > well. > > There may be a reason for that. > > > My goals were to create something that felt very close to python > > modules, and be natural in the JavaScript world. [examples] > > This is where the problem starts. > > The main issue here is that even the word namespace creates a false > illusion, that Javascript can be treated similar to other languages. > > The hierarchically construction is quite appealing I can't argue that, > but it's very inefficent to use deeply nested chains of objects. They > come with a cost because of the way they work (property lookups). > > The biggest problem here isn't the tools/libraries, but the people who > tend to misuse them. > > Recommended reading:http://peter.michaux.ca/articles/javascript-namespacing > > - Bal zs > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit > here:http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
