[Home]Javascript/Namespaces

UseModWiki | Javascript | RecentChanges | Preferences

Namespaces

Recapturing from [/Modules]:

 var difference = import_module( "difference" ).difference;
 var union = import_module( "union" ).union;
 union( [1, 2, 3, 4], difference( [5, 6, 7], [ 5, 6 ] ) );

The functions names "union" and "difference" aren't very specific, the better names for them would be "set_union" and "set_difference", or we can put these union and difference in the speical namespace:

  sets.union( [1, 2, 3, 4], sets.difference( [5, 6, 7], [ 5, 6 ] ) );
The above is much clearer (In my opinion fully qualified names are always preferred to not fully-qualified ones). To support that we would need:
 var sets = {}
 sets.difference = import_module( "difference" ).difference;
 sets.union = import_module( "union" ).union;

One simplification can easily be made. If module knows about the symbols it exports, and it is ok for it to know into what namespace they will be imported, then we can just write:

 import_module( "union" ) 
and then use it as
 sets.union( [ 1, 2, 3], [ 3, 5, 6 ] )
or event better:
 import_module( "sets.union" ) 
 sets.union( [ 1, 2, 3], [ 3, 5, 6 ] )

where <Full module name> ::= <namespace the module exports names to> "." <module name>. In the example above the namespace is "sets" and the module name is "union". The <module name> is not the same as the symbols module exports:

 import_module( "sets.basic_ops" ) 
 sets.union( [ 1, 2, 3 ], sets.difference( [ 3, 4, 5 ], [ 3 ] ) );
sets.basic_ops exports both "union" and "difference"
 module( "sets.basic_ops", function() 
     {
     function union() {}
     function difference {}

     return  { union: union, difference: difference };
     } );

Comment

I'm just learning uh... *script, and as a learning tool I'm writing a javascript to native code compiler, using C++ as the backend. Not for web pages, for general purpose programming. I'm obviously very interested in extensions that support modules and namespaces, but I'm kind of horrified at some of the proposed language additions I'm reading about. They seem like such a violation of the spirit of the language. Wouldn't it be more in the spirit of javascript to do something like:

 var Sets = Module.import("SetOperations?");
 Sets.difference...

Defining the import function as taking a module name (ala Python) and returning an object whose properties are all the (enumerable) members of the imported module? And I would define a module as any file of javascript code - as in Python, there is no requirement to bracket a module with special syntax. Note that a reference to setThing in the definition of difference would be resolved, in the spirit of javascript, lexically - to the setThing of the module in which difference was defined.

When you want unqualified access to some members, you just pull them out as usual:

 var difference = Sets.difference;

Or you can easily create a function that takes all the members of a module and promotes them into the current scope. This is a common feature of languages with an 'import' feature, but in javascript, it requires no new syntax or semantics!

Visibility: If there is a way to mark properties as non-enumerable, then a module could use that to hide certain of its members during import.

Can anybody suggest a forum where I could find other *script implementors to talk to? Spike0xff <spike@dosadi.com>


UseModWiki | Javascript | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited August 6, 2005 10:27 pm by Spike0xff (diff)
Search: