Subj : Re: Creating classes/namespaces on the fly from existing functions? To : Shanti Rao From : Brendan Eich Date : Tue Jan 11 2005 04:06 pm Shanti Rao wrote: > >>> f = getLibrary("foo.js") >>> includeLibrary("bar.js") >>> >>> With the former, there are a few minor glitches. Consider >>> >>> js>f = {Foo: function() {this.message = 'Foo'}}; >> >> >> >> But why must getLibrary be implemented so as to use anonymous >> functions if "foo.js" contains named function declarations? >> >> /be >> > > That is exactly the problem with using an object when you really wanted > a namespace. > > For the sake of argument, assume that "foo.js" contains a named function > Foo(), and getLibrary() makes Foo() (formerly a global object in foo.js) > into a property of an object when included in another script. Sure, but Foo is still a named function; its name is intrinsic. That preserves the invariant you seemed to want last time (a.constructor.name being defined). > Now that I think about it some more, it's sufficiently confusing to > promote the minor glitch into a major irritation. It makes it very > difficult to convert a stand-along script into a library. This > conception of a getLibrary() function will probably be a failure. You're right that other things break in such a case. That's why JS2 has packages that you import, including things like explicit, to restrict the use of unqualified imported names. > An include() function still works, so long as you don't unintentionally > use the same global variable name in two different source files. This > highlights the fact that creating a class is very different from > creating a namespace. Or a package, which is like an object and a namespace, not just one or the other. > Shanti > > PS. Straw man: what about a 'library' as a separate thread with a > COM-like messaging interface? A synchronous function call would send a > message and wait for a reply. Or not wait. It invokes buzzwords like > grid computing and transaction processing. I don't see why threads should be dragged in implicitly. /be .