Subj : Re: Creating classes/namespaces on the fly from existing functions? To : Shanti Rao From : Brendan Eich Date : Mon Jan 10 2005 03:04 pm Shanti Rao wrote: >> Now I do it like this: >> var x=getLibrary("foo.js"); >> >> and getLibrary returns an (module) object. (the newly created (module) >> object is passed as this to the foo.js to allow the script to decide if >> it still wants to pollute the global namespace or not) >> >> This gets quite close to real namespaces. >> >> Jens >> > > I like the idea of encapsulating library variables in thir own > namespace. An interpreter would have to have two functions, since some > code is intended to run inline: > > 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 > js>a = new f.Foo() > [object Object] > js>a.constructor.name > > The last statement returns an empty string. Compare with > > js>function Bar() {this.message='Bar'}; > js>b = new Bar > [object Object] > js>b.constructor.name > Bar > > Shanti > > > > .